[英]Showing div after scrolling doesn't work
我想在滚动150px后显示“转到顶部” div,但我真的不知道如何...我尝试了很多脚本,但没有任何效果。 这是我的HTML:
<div id="goto_top" onClick="gotoId('top')" title="Иди на врх"></div>
JS:
$(document).scroll(function () {
var h = $(this).scrollTop();
if (h > 150) {
$('#goto_top').fadeIn();
} else {
$('#goto_top').fadeOut();
}
});
CSS:
#goto_top{
position: fixed;
background:url(../images/goto_top.png) transparent no-repeat;
background-position: top center;
padding:5px;
width:35px;
height:25px;
bottom:40px;
right:50px;
cursor: pointer;
}
#goto_top:hover, #goto_top:active{
background-position: bottom center;
}
问题是什么?
尝试将$(document)更改为$(window)。 另外,您应该研究功能的反跳功能,这种功能在这种情况下非常有用,因为在这种情况下,您需要检查一些滚动位置,但是您不想持续不断地进行滚动操作,因为这会导致浏览器的大量开销。 您可以将该功能反跳100毫秒,并且对于用户来说似乎仍然是瞬时的,同时减少了该过程中的大量浏览器开销。
我在这里创建了一个示例: jsfiddle.net/6eF9g/1/
$(window).scroll(function () {
if ($(window).scrollTop() >= 150) {
$(".gotoTop").fadeIn();
} else {
$(".gotoTop").fadeOut();
}
});
$(".gotoTop").hide();
$(".gotoTop").click(function () {
$("body,html").animate({
scrollTop: 0
}, 600);
return false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.