2015-09-28 66 views
0

我需要在用戶滾動幾乎在頁面的底部,然後直到某個點後專門觸發一個函數。我如何正確地做到這一點?複雜的jquery scoll

Image as helper

advertisement.refreshAd = function() { 
 
    $(window).scroll(function(event) { 
 
    if('almost at the end then scroll up to certain point') 
 
    {console.log('trigger refresh!');} 
 
    }); 
 
}

回答

0
advertisements.refreshAd = function() { 
    var furthestPoint = false; 
    var point2 = 1700 //will be dynamic; 

    var lastScrollTop = 0; 
    var scrolledToPosition = false; 

    $(window).scroll(function() { 
     var st = $(this).scrollTop(); 

     if($(window).scrollTop() + $(window).height() > $(document).height() - 100) { 
      furthestPoint = true; 
     } 
     if (st < lastScrollTop && st < point2 && furthestPoint){ 
      if (!scrolledToPosition) { 
       console.log('TRIGGER! - once only!'); 
       scrolledToPosition = true; 
      } 
     } 

     lastScrollTop = st; 
    }); 
}; 
advertisements.refreshAd(); 
0

你所尋找的是jQuery函數scrollTop()。該守則將是這個樣子:

var atEnd = false; 
 
advertisement.refreshAd = function() { 
 
    $(window).scroll(function(event) { 
 
     if($(document).scrollTop() > $(document).height()-500) { //point1 
 
      atEnd = true; 
 
     } if (($(document).scrollTop() < point2) && atEnd){     
 
      console.log('trigger refresh!'); 
 
     } 
 
    }); 
 
}

0

試試下面的代碼

<script> 
     var touchedBottom = 0; 
     jQuery(window).scroll(function(event) { 
      if(jQuery(window).scrollTop() + jQuery(window).height() == jQuery(document).height()) { 
      touchedBottom = 1; 
      console.log("touched Bottom"); 
      } 
      if(touchedBottom){ 
       if(jQuery(window).scrollTop() < jQuery("#point_2").offset().top){ 
        touchedBottom = 0; 
        console.log("touched point 1"); 
       } 
      } 
     }); 
    </script>