解决iscroll.js上拉下拉刷新手指划出屏幕页面无法回弹问题
2016-02-17 23:52
666 查看
博客已迁移至http://zlwis.me。
使用过iscroll.js的上拉下拉刷新效果的朋友应该都碰到过这个问题:在iOS的浏览器中,上拉或下拉刷新时,当手指划出屏幕后,页面无法弹回。很多人因为解决不了这个问题,干脆就那样不解决了,还有的直接就不用HTML了,使用原生代替HTML页面。
相信很多朋友也有自己的解决办法,只是没写出来,所以网上都搜不到解决方案。在很多QQ群里面也有很多人在问该怎么解决这个问题,所以我写这篇文章记录一下我的解决方案,希望对一些朋友有所帮助。
上拉下拉刷新的主要代码:
页面无法弹回的原因在于:手指划出屏幕后
在
下面解释一下这段代码的意思。
当
下拉过程也可以同理分析。
欢迎留下你的解决方法。
使用过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 = 'flip'; pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Release to refresh...'; this.maxScrollY = this.maxScrollY; } 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 < this.maxScrollY) && (this.pointY < 1)){ this.scrollTo(0, this.maxScrollY, 400); return; } else if (this.y > 0 && (this.pointY > window.innerHeight - 1)) { this.scrollTo(0, 0, 400); return; } ...... }
下面解释一下这段代码的意思。
this.y是页面已经滚动的垂直距离,
this.maxScrollY是最大垂直滚动距离,
this.pointY手指当前的垂直坐标。
当
this.y < this.maxScrollY,就是已经处于上拉的过程,当
(this.y < this.maxScrollY) && (this.pointY < 1)时,处于上拉且手指已经触及屏幕边缘,这时候手动触发
this.scrollTo(0, this.maxScrollY, 400),页面就开始回弹。
下拉过程也可以同理分析。
欢迎留下你的解决方法。
相关文章推荐
- 【JavaScript】JS读取XML文件并进行搜索
- XSS中JavaScript加密以及Filter bypass
- html锚点的作用和js选项卡锚点跳转的使用
- 利用js参数,保持客户端文件的新鲜度
- JSP 的“4379”
- 自学javascript笔记_自用_解析W3school的代码
- 第2章{ 2-14 [选学]读懂bee的gulpfile.js }
- 第2章{ 2-13 使用抓包工具线上调试 }
- 第2章{ 2-12 使用gulp构建kissy工程 }
- 第2章{ 2-10 使用依赖表控制combo }
- js模拟Ctrl+C复制、Ctrl+V粘贴功能之tabindex、focus、keyCode
- 第2章{ 2-9 别名机制 }
- 第2章{ 2-8 使用combo配置减少请求 }
- 封装通信接口数据的方法-json
- 第2章{ 2-6 包配置 }
- javascript console 函数详解 js开发调试的利器
- 第2章{ 2-5 模块使用 }
- 第2章{ 2-4 Kissy模块规范 }
- JSP总结1
- 第2章{ 2-3 一个模块一个js文件 }