javascript - 实现“滚动到顶部”按钮
问题描述
在我的网站上有一个“返回顶部”功能:
https://www.publictalksoftware.co.uk/
向下滚动页面,然后您将看到它。现在,此功能已作为我在 Wordpress 中使用的主题的一部分实现。
我想在另一个站点的另一个页面上使用相同的功能来学习。我试图找到我需要的所有位,我已经做到了这一点:
http://trucklesoft.co.uk/test/backtotop.php#
我遇到的问题是:
- 盒子大小不一样
- 雪佛龙的排列方式不同
- 当我点击 V 形时没有任何反应
- 该框始终可见
我在这里缺少哪些步骤?
更新
根据评论,我添加了几script
行。我现在在我的head
部分有这个:
<head>
<link href="/test/fa/css/all.css" rel="stylesheet"> <!--load all styles -->
<script defer src="/test/fa/js/all.js"></script> <!--load all styles -->
<script type='text/javascript' src='test/js/jquery/jquery.js?ver=1.12.4'></script>
<script type='text/javascript' src='test/js/jquery/jquery-migrate.min.js?ver=1.4.1'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var cnArgs = {"ajaxurl":"https:\/\/www.publictalksoftware.co.uk\/wp-admin\/admin-ajax.php","hideEffect":"fade","onScroll":"no","onScrollOffset":"100","cookieName":"cookie_notice_accepted","cookieValue":"true","cookieTime":"2592000","cookiePath":"\/","cookieDomain":"","redirection":"","cache":"","refuse":"no","revoke_cookies":"0","revoke_cookies_opt":"automatic","secure":"1"};
/* ]]> */
</script>
<style type="text/css">
#sv-totop {
position: fixed;
right: 40px;
bottom: 65px;
display: none;
outline: none;
background: #d35438 !important;
-moz-box-shadow: inset 0 30px 30px -30px #7F7F7F, inset 0 -30px 30px -30px #7F7F7F;
-webkit-box-shadow: inset 0 30px 30px -30px #7F7F7F, inset 0 -30px 30px -30px #7F7F7F;
box-shadow: inset 0 30px 30px -30px #606060, inset 0 -30px 30px -30px #606060;
width: 45px;
height: 45px;
text-align: center;
color: #E7D8A3 !important;
padding: 8px;
font-size: 20px;
-webkit-transition: all 0.1s linear 0s;
-moz-transition: all 0.1s linear 0s;
-o-transition: all 0.1s linear 0s;
transition: all 0.1s linear 0s;
font-family: 'Tahoma', sans-serif;
z-index: 99999999;
}
#sv-totop:hover {
opacity: 0.8;
-moz-box-shadow: inset 0 0 20px #000000;
-webkit-box-shadow: inset 0 0 20px #000000;
box-shadow: inset 0 0 20px #000000;
}
</style>
<script type="text/javascript">
var colomatduration = 'fast';
var colomatslideEffect = 'slideFade';
var colomatpauseInit = '';
var colomattouchstart = '';
</script>
<script type="text/javascript">
jQuery(document).ready(function($){
$(window).scroll(function () {
if ( $(this).scrollTop() > 500 )
$("#sv-totop").fadeIn();
else
$("#sv-totop").fadeOut();
});
$("#sv-totop").click(function () {
$("body,html").animate({ scrollTop: 0 },1000 );
return false;
});
});
</script>
<script type="text/javascript">
jQuery(document).ready(function() {
});
</script>
</head>
它已经有所改善。但是现在它会跳转并且不会滚动。我不明白为什么我的 javascript 没有被调用。
解决方案
看看本指南,我认为它可能会很有帮助How TO - Scroll Back To Top Button
// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
document.getElementById("myBtn").style.display = "block";
} else {
document.getElementById("myBtn").style.display = "none";
}
}
$('#myBtn').on('click', function (e) {
e.preventDefault();
$('html,body').animate({
scrollTop: 0
}, 700);
});
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 20px;
}
#myBtn {
display: none;
position: fixed;
bottom: 20px;
right: 30px;
z-index: 99;
font-size: 18px;
border: none;
outline: none;
background-color: red;
color: white;
cursor: pointer;
padding: 15px;
border-radius: 4px;
}
#myBtn:hover {
background-color: #555;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="myBtn" title="Go to top">Top</button>
<div style="background-color:black;color:white;padding:30px">Scroll Down</div>
<div style="background-color:lightgrey;padding:30px 30px 2500px">This example demonstrates how to create a "scroll to top" button that becomes visible when the user starts to scroll the page.</div>
推荐阅读
- java - 调用jar时将java日期等效为php日期函数
- kubernetes - 在 Kubernetes 中使用 traefik 作为负载均衡器的问题
- mysql - 如何在 Mysql 中将日期时间转换为简单日期?
- angular - 如何在 Angular 8+ 中实现 Pendo
- python - 加载管道仅返回列名
- javascript - 提供从 Siri 语音输入到可编写脚本或 pythonista 脚本的参数?
- mongodb - MongoDB 接近 PaaS 上的最大内存
- python - 使用变量访问 python 类中的函数
- wolfram-mathematica - SCOM 的 MMASetup 命令行参数
- javascript - JS:正则表达式 SyntaxError:无效的正则表达式:无效的组说明符名称