简体   繁体   中英

effect similar to infinite scroll for javascript/jquery

I want to achieve something similar to infinite scroll, but I want it to trigger when an element is in the viewable window rather than scroll position. Any ideas?

You can use JQuery's offset() to see if an element is on screen or not:

http://api.jquery.com/offset/

I have a few little static helper functions in a Utils class for occassions such as these:

Utils = {
    underView: function(element) {
    return (($(window).height() + $(window).scrollTop()) <= element.offset().top);
},

aboveView: function(element) {
    return ($(window).scrollTop() >= element.offset().top + element.height());
},

inView: function(element) {
    return (Utils.aboveView(element) !== true && Utils.underView(element, element.height()) !== true);
}

};

Implemented thusly:

$(window).scroll(function(){
    if(Utils.inView($(".div"))){
        // do something
    }
});

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