javascript 100px后的jQuery滚动功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10259132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Scroll function after 100px
提问by Bram Vanroy
Using this script:
使用这个脚本:
<script>
$(function() {
$(window).scroll(function(){
$('#Your element id').slideUp('slow');
});
});
</script>
Is it possible only to perform the action after the user has scrolled 100px or more?
是否只能在用户滚动 100 像素或更多后执行操作?
回答by Bram Vanroy
You do need scrollTop as said. It would be wise to include an 'else' function as well, so that when you scroll back to the top the toggled element gets hidden again. As such:
如上所述,您确实需要 scrollTop。最好还包含一个“else”函数,这样当您滚动回顶部时,切换的元素会再次隐藏。像这样:
$(document).ready(function() {
$('#scrollDiv').hide();
$(window).scroll(function() {
if ($(document).scrollTop() > 100) {
$('#scrollDiv').fadeIn('slow');
}
else {
$('#scrollDiv').fadeOut('slow');
}
});
});?
Here is a quick jsfiddle
这是一个快速的jsfiddle
回答by Josh Davenport
You can use .scrollTop()
to get how far the page has been scrolled
您可以使用.scrollTop()
获取页面滚动的距离
<script>
$(function() {
$(window).scroll(function(){
if($(this).scrollTop() > 100) {
$('#Your element id').slideUp('slow');
}
});
});
</script>