admin管理员组

文章数量:1398766

I need to detect if user scrolled to the bottom of the page so I can make some action.
But I did something wrong and when I scroll to the bottom nothing happens but when I scroll to the top it fires the action :(.
And should opposite :)

$(window).scroll(function () {        
        if ($(window).scrollTop() == $(document).height() - $(window).height() && !($('#imgLoad').is(':visible'))) {
            alert('you hit bottom');
            loadMore();
        }
    });

I need to detect if user scrolled to the bottom of the page so I can make some action.
But I did something wrong and when I scroll to the bottom nothing happens but when I scroll to the top it fires the action :(.
And should opposite :)

$(window).scroll(function () {        
        if ($(window).scrollTop() == $(document).height() - $(window).height() && !($('#imgLoad').is(':visible'))) {
            alert('you hit bottom');
            loadMore();
        }
    });
Share Improve this question edited Jan 4, 2021 at 21:16 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 8, 2013 at 14:15 11101110 6,85956 gold badges187 silver badges346 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 4

please try this one

$(window).scroll(function() {
        if($(window).scrollTop() + $(window).height() == $(document).height()) {
       alert("bottom!");
   }
});

you can also have another option on the following question: Check if a user has scrolled to the bottom

This is an old post, but posting in case this helps someone else in the future. I was experiencing the same problem. The event would fire as I scrolled to the top of the page from the bottom. Using document.body.clientHeight instead of $(window).height fixed things for me. This was tested using Chrome.

$(window).scroll(function() {
    if($(window).scrollTop() + document.body.clientHeight == $(document).height()) {
        alert('bottom of page');
    }
});
if (document.body.offsetHeight + document.body.scrollTop>= document.body.scrollHeight){
    alert("bottom!");
}
$(window).on("scroll", function() {
    var scrollHeight = $(document).height();
    var scrollPosition = $(window).height() + $(window).scrollTop();
    if ((scrollHeight - scrollPosition) / scrollHeight === 0) {
     // when scroll to bottom of the page
    }
});
   if($(window).scrollTop() + $(window).height() > $(document).height()-200) {
                alert();
                // add your custom function here. i.e what you want to do.
            }

This will work.The user scrolls down the page and when the if(condition) is met the code is executed.

Please try this.

following scroll function would be worked while window scrolling bottom in asp
$(window).scroll(function() {    
$(window).scrollTop() + document.body.clientHeight == $(document).height()
});

and following scroll function would be worked while window scrolling bottom in html or mvc page view.
$(window).scroll(function() {    
$(window).scrollTop() == $(document).height() - $(window).height()
});

本文标签: javascriptCheck if user scroll to bottom of the page work oppositeStack Overflow