使用过iscroll.js的上拉下拉刷新效果的朋友应该都碰到过这个问题:在ios的浏览器中,上拉或下拉刷新时,当手指划出屏幕后,页面无法弹回。很多人因为解决不了这个问题,干脆就那样不解决了,还有的直接就不用html了,使用原生代替html页面。
相信很多朋友也有自己的解决办法,只是没写出来,所以网上都搜不到解决方案。在很多qq群里面也有很多人在问该怎么解决这个问题,所以我写这篇文章记录一下我的解决方案,希望对一些朋友有所帮助。
上拉下拉刷新的主要代码:
myscroll = new iscroll('wrapper', { vscrollbar: false, usetransition: true, topoffset: pulldownoffset, onrefresh: function () { if (pulldownel.classname.match('loading')) { pulldownel.classname = ''; pulldownel.queryselector('.pulldownlabel').innerhtml = 'pull down to refresh...'; } else if (pullupel.classname.match('loading')) { pullupel.classname = ''; pullupel.queryselector('.pulluplabel').innerhtml = 'pull up to load more...'; } }, onscrollmove: function () { if (this.y > 5 && !pulldownel.classname.match('flip')) { pulldownel.classname = 'flip'; pulldownel.queryselector('.pulldownlabel').innerhtml = 'release to refresh...'; this.minscrolly = 0; } else if (this.y < 5 && pulldownel.classname.match('flip')) { pulldownel.classname = ''; pulldownel.queryselector('.pulldownlabel').innerhtml = 'pull down to refresh...'; this.minscrolly = -pulldownoffset; } else if (this.y (this.maxscrolly + 5) && pullupel.classname.match('flip')) { pullupel.classname = ''; pullupel.queryselector('.pulluplabel').innerhtml = 'pull up to load more...'; this.maxscrolly = pullupoffset; } }, onscrollend: function () { if (pulldownel.classname.match('flip')) { pulldownel.classname = 'loading'; pulldownel.queryselector('.pulldownlabel').innerhtml = 'loading...'; pulldownaction(); } else if (pullupel.classname.match('flip')) { pullupel.classname = 'loading'; pullupel.queryselector('.pulluplabel').innerhtml = 'loading...'; pullupaction(); } } });
页面无法弹回的原因在于:手指划出屏幕后touchend事件无法触发,回弹动画就无法执行。解决办法就是:当手指接近屏幕边缘的时候,手动触发动画方法。
在onscrollmove方法中插入判断代码:
onscrollmove: function () { if((this.y window.innerheight - 1)) { this.scrollto(0, 0, 400); return; } ...... }
下面解释一下这段代码的意思。
this.y是页面已经滚动的垂直距离,this.maxscrolly是最大垂直滚动距离,this.pointy手指当前的垂直坐标。
当this.y
下拉过程也可以同理分析。