简体   繁体   中英

JQuery window scroll top and bottom

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
    }
});

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())){

            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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