8 个回答

我也是找了好久这个。看了楼上@海农的答案。想了想按照如下方式应该更好一点。
代码中 this.y-minY>10 这里的10是加载阈值(可以理解为滚动元素下边缘离开父容器下边缘),可以实际情况自行调整。

myScroll.on('scrollStart', function() {
        
  minY = this.y;
  // console.log(this);
});

myScroll.on('scroll', function() {
        
  minY = minY<this.y ? minY : this.y;
  // console.log(this);
});

myScroll.on('scrollEnd', function() {
    minY = minY<this.y ? minY : this.y;
  //
  if (this.y-minY>10 && (this.directionY===1)) {
    //加载
  }
});

只在scrollStart和scrollEnd中判断有个小情况应该是不能处理,假如滚动元素目前位置没有达到最底部,猛的向上一划,最底下的元素会滑动到离开父容器底部一段距离,这时候应该加载新数据,但是pullStart 并不等于 this.y,所以不会加载新数据。

这个需要自己监听滚动事件判断的。监听 iScroll的scrollEnd事件并计算。
文档

如果wrapper嵌套在别的标签里,其父集position不可是设置为relative或者absolute

如果你用到jquery,可以用jquery判断网页到达底部,如果没有用到,你也可以按照这个思路判断:

 $(window).scroll(function(){
    var scrollTop=$(this).scrollTop();
    var scrollHeight=$(document).height();
    var windowHeight=$(this).height();
    if(scrollTop+windowHeight===scrollHeight){
      alert('bottom');
    }
  });

看了amazeui中使用iscroll是这么定义,在iscroll的事件scrollStart的时候,存储this.y到一个变量(pullStart),然后在scrollEnd的时候去判断pullStart是否等于当前的this.y,如果相等,再结合directionY的方向判断是否是上滑,是的话,说明是到底了。。。

        myScroll.on('scrollStart', function() {
        
          pullStart = this.y;
          // console.log(this);
        });

        myScroll.on('scrollEnd', function() {

          // pull up to load more
          if (pullStart === this.y && (this.directionY === 1)) {
            //加载
          }
        });
      };

根据@HuFeiyan 写的代码,我优化了一下。
首先要引入iscroll-probe.js,然后new的时候要加下参数probeType:3,这两点缺一不可!
new IScroll('.mainContent', { scrollY: true, click: true, bounce: false, probeType: 3,mouseWheel: true});

myScroll.on('scrollStart', function() { 
  minY = this.y;
});

myScroll.on('scroll', function() {
  minY = minY<this.y ? minY : this.y;
});

myScroll.on('scrollEnd', function() {
  if (this.y-minY<10 && (this.directionY===1)) {
    //加载
  }
});
if (this.options.iosScrollBottomCallback) {
            this.hasScrollToBottom = false;
            myIscroll.on('scrollEnd', () => {
              if (Math.abs(myIscroll.y - myIscroll.maxScrollY) < 20 && myIscroll.directionY === 1 && !this.hasScrollToBottom) {
                if (typeof this.options.iosScrollBottomCallback === 'function') {
                  this.options.iosScrollBottomCallback();
                  this.hasScrollToBottom = true;
                }
              }
            });
          }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题