Javascript 使用jquery创建返回顶部按钮,以解决代码中的问题
我在我的Javascript 使用jquery创建返回顶部按钮,以解决代码中的问题,javascript,jquery,html,css,scrolltop,Javascript,Jquery,Html,Css,Scrolltop,我在我的index.php上有一个固定的按钮,当用户位于页面底部时,这个按钮就可以转到顶部 <button id="fixed-btn"></button> 为此,我编写了jquery $(document).ready(function() { function testScroll(ev) { if(window.pageYOffset>400) { $('#fixed-btn').addClass('show');
index.php
上有一个固定的按钮,当用户位于页面底部时,这个按钮就可以转到顶部
<button id="fixed-btn"></button>
为此,我编写了jquery
$(document).ready(function()
{
function testScroll(ev)
{
if(window.pageYOffset>400)
{
$('#fixed-btn').addClass('show');
}
else
{
$('#fixed-btn').removeClass('show');
}
}
window.onscroll=testScroll
$("#fixed-btn").click(function()
{
$('html, body').animate(
{
scrollTop:0
}, 1500 );
});
我不知道为什么它不工作。按钮不可见。任何人都可以有关于它的想法,然后请分享。提前感谢:)我看到了你的代码,我认为这会解决你的问题。用下面给出的替换你的css
#fixed-btn{
position: fixed;
bottom: 50px;
right: 50px;
height: 30px;
width: 30px;
border-radius: 100%;
background-color: red;
z-index: 9999;
opacity: 0;
}
只需添加z-index:9999代码>在此#固定btn{..}
如果仍然面临问题,请告诉我。您尝试过css中的z-index吗@foramIt对我来说很好。检查您是否错过了这个})代码>在末尾
#fixed-btn{
position: fixed;
bottom: 50px;
right: 50px;
height: 30px;
width: 30px;
border-radius: 100%;
background-color: red;
z-index: 9999;
opacity: 0;
}