admin管理员组

文章数量:1332604

I am able to load my ajax when scrolling all the way to the bottom, i am trying to figure out how i can modify the piece of code below so that it works only when the window is scrolled to the top ?

 $(window).scroll(function () {
    if ($(document).height() <= $(window).scrollTop() + $(window).height()) {
       //this works here for scrolling bottom
    }
    else if ($(document).height() >= $(window).scrollTop() + $(window).height()){

        //i tried checking for greater than the window scroll but that didn't owrk
    }
});

I am able to load my ajax when scrolling all the way to the bottom, i am trying to figure out how i can modify the piece of code below so that it works only when the window is scrolled to the top ?

 $(window).scroll(function () {
    if ($(document).height() <= $(window).scrollTop() + $(window).height()) {
       //this works here for scrolling bottom
    }
    else if ($(document).height() >= $(window).scrollTop() + $(window).height()){

        //i tried checking for greater than the window scroll but that didn't owrk
    }
});
Share Improve this question asked Nov 27, 2013 at 2:55 WarzWarz 7,77615 gold badges72 silver badges122 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

When the scrollTop() returns the vertical position of the scroll bar 0 it means the scroll bar is in top position.

$(window).scroll(function () {
    if ($(window).scrollTop() == 0){
        alert("Up");
    }
});

Or you can update your code as follows,

$(window).scroll(function () {
    if ($(window).scrollTop() + $(window).height() < $(document).height()){
        alert("Up");
    //i tried checking for greater than the window scroll but that didn't work
    }
});

Check this or perhaps you should check if height of document and window object to make sure they're not null.

 $(window).scroll(function () {
            if ($(document).height() <= Number($(window).scrollTop() + $(window).height())) {
               //this works here for scrolling bottom
            }
// only greater i think, not equa
            else if ($(document).height() > Number($(window).scrollTop() + $(window).height())){

            }
        });

本文标签: javascriptJQuery window scroll top and bottomStack Overflow