gpt4 book ai didi

javascript - 实现 "scroll to top"按钮

转载 作者:太空宇宙 更新时间:2023-11-04 07:12:27 25 4
gpt4 key购买 nike

我的网站上有一个“返回顶部”功能:

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>

关于javascript - 实现 "scroll to top"按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51040798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com