Trigger events when the window is scrolled to certain positions

后端 未结 4 1187
别那么骄傲
别那么骄傲 2020-12-01 10:11

I would like to call functions when the browser window goes beyond a certain point
(e.g the user scrolled the window down beyond 200px from the top

Is there an e

4条回答
  •  渐次进展
    2020-12-01 10:37

    if you dont want to use plugin you may try this

    function isScrolledIntoView(elem)
            {
            //alert("method invoked");
            var docViewTop = $(window).scrollTop();
            var docViewBottom = docViewTop + $(window).height();
            var elemTop = $(elem).offset().top;
            var elemBottom = elemTop + $(elem).height();
            return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom) && (elemBottom <=      docViewBottom) && (elemTop >= docViewTop));
    
            }
    

    I had used this function to load images as lazy load. Called this function on scroll and then sent an ajax request based on the value of function returning value. this will send the ajax request when the user had scrolled to the bottom of the element (i.e used as the parameter in the method).

    if(isScrolledIntoView($('.items:last'))){
        //send the ajax request here 
    }
    

    this is my first answer on this site. I hope you have will understand

提交回复
热议问题