如何解决Android设备上touchend不能触发bug
来源:互联网 发布:淘宝定时上架什么意思 编辑:程序博客网 时间:2024/06/16 22:58
移动项目开发过程中,经常需要用到滑动的事件来处理一些效果。通常情况下,我们会通过 touchstart->touchmove->touchend 的过程来定义这个事件。这些事件的触发顺序是 touchstart, touchmove, touchmove ….. touchend 。绝大部分平板或手机也正如我们想象的那样有序执行着。但是以Android 4.0.4为首的一些可恶分子却有些不听话:他们的touchend事件没有如预期的那样触发。
监听这些事件我们会发现,当只是轻点一下屏幕时,touchend可以正常触发。但是只要当 touchmove 被触发之后,touchend 就不会再被触发了,而且 touchmove 也没有持续触发。
在网上搜集了一些资料显示,这是 Android 上浏览器的bug
> On Android ICS if no preventDefault is called on touchstart or the firsttouchmove,
> further touchmove events and the touchend will not be fired.
正如提到的我们只需要在 touchstart 或者 touchmove 里执行一下 e.preventDefault(); 就可以避免这个bug。但是,问题来了:添加了 preventDefault 之后,正常的scroll事件也被屏蔽了!我们意外的发现滚动条也不能用了!
于是,我们开始尝试各种添加preventDefault事件的时机:闭包,延迟,判断等一一用上。最终焦点落在了firsttouchmove上,于是有了以下代码。
var touchY = 0;$(this).on('touchstart', function(e){ var touch = e.touches[0]; touchY = touch.clientY;}).on('touchmove', function(e){ var touch = e.touches[0] if(Math.abs(touch.clientY - touchY) < 10){ e.preventDefault(); }}).on('touchend', function(){ // 你的滑动事件});
基本上主要的思想就是在 touchmove 的时候判断出纵轴的位移量,当小于某个值的时候就认为是在执行左右滑动,且需要执行 preventDefault 来确保 touchend 可以正常触发。当然,如果你有横向滚动条且想绑定上下滑动事件的话就需要自己修改一下代码。
参考:
http://blog.mobiscroll.com/working-with-touch-events/.
- 如何解决Android设备上touchend不能触发bug
- 解决滑动时不触发touchend事件的bug
- 手机网页如何取消常按选中功能和touchstart后不能触发touchend事件
- 解决移动端页面滚动后不触发touchend事件
- 解决移动端页面滚动后不触发touchend事件
- 如何让touchmove之后不触发touchend的事件
- Android开发丶解决应用不能在Android7.0+设备上不能安装的问题
- 如何利用Sanitizer解决Android中的Bug?
- 如何在android设备上insmod自己单独编译的.ko,解决签名问题
- Android开发 解决RecyclerView in SwipeRefreshLayout触发下拉刷新的bug
- Android设备在Mac上不能真机调试
- 如何解决 WebSphere MQ V6.0 通道不能被触发的问题?
- Android-完美解决在Activity中触摸返回键onBackPressed不能触发问题
- 关于touch事件的使用 (touchStart touchMove touchEnd(不触发 android 4.0以上)) 滑动的使用
- 如何解决Bug
- 如何高效率解决BUG
- 遇到BUG如何解决
- UISlider小范围滑动不能触发抬起事件的BUG
- EffectC++(二):构造/析构/赋值运算
- 算法笔记——【分治法】循环赛事日程表
- Fragment生命周期
- 关于安卓clean项目后没有R文件的解决
- 修饰符和static用法
- 如何解决Android设备上touchend不能触发bug
- js解析GET参数解析一次性解决
- 09 GUI
- Android自定义View控件
- (4)Jfreechart之IntervalBarChart(显示差值的图表)
- 【论文笔记】Leveraging Datasets with Varying Annotations for Face Alignment via Deep Regression Network
- linux shell中的变量
- CDUESTC 2016 假期赛1 D题
- GCD总结0