复杂的jquery滚动

时间:2015-09-28 10:57:21

标签: javascript jquery scroll

我需要在用户几乎在页面底部滚动然后直到特定点之后才触发一个功能。我该怎么做呢?

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

3 个答案:

答案 0 :(得分: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!');
      }
   });
}
&#13;
&#13;
&#13;

答案 1 :(得分: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>

答案 2 :(得分: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();