简体   繁体   English

100px之后的jQuery Scroll功能

[英]jQuery Scroll function after 100px

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? 是否可以仅在用户滚动100px或更多后执行操作?

You do need scrollTop as said. 你确实需要scrollTop。 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. 包含'else'函数也是明智之举,这样当你向后滚动到顶部时,切换元素会再次被隐藏。 As such: 因此:

$(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

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM