Load ajax when scroll reaches 80%

前端 未结 2 1107
深忆病人
深忆病人 2020-12-04 07:24

I am using the following code which is working when the scroll bar reaches the botttom,

if($(window).scrollTop() == $(document).height() - $(window).height(         


        
2条回答
  •  情书的邮戳
    2020-12-04 08:27

    A quick google search for get percentage scrolled down brings up this page as the first result(with the code below, which more or less does what you want). I feel like you didn't attempt any research before asking here.

    $(document).scroll(function(e){
    
        // grab the scroll amount and the window height
        var scrollAmount = $(window).scrollTop();
        var documentHeight = $(document).height();
    
        // calculate the percentage the user has scrolled down the page
        var scrollPercent = (scrollAmount / documentHeight) * 100;
    
        if(scrollPercent > 50) {
            // run a function called doSomething
           doSomething();
        }
    
        function doSomething() { 
    
            // do something when a user gets 50% of the way down my page
    
        }
    
    });
    

提交回复
热议问题