资讯专栏INFORMATION COLUMN

移动端触摸、点击事件优化(fastclick源码学习)

fxp / 1980人阅读

摘要:移动端触摸点击事件优化源码学习最近在做一些微信移动端的页面,在此记录关于移动端触摸和点击事件的学习优化过程,主要内容围绕展开。当指针设备通常指鼠标在元素上移动时事件被触发。移动端有延迟问题,可没有哦。

移动端触摸、点击事件优化(fastclick源码学习)

最近在做一些微信移动端的页面,在此记录关于移动端触摸和点击事件的学习优化过程,主要内容围绕fastclick展开。
fastclick github

问题起源

</>复制代码

  1. 移动端浏览器一般在用户点击屏幕之后会延迟大约300ms才触发click event
    ——GOOGLE

手机打开此链接查看延迟demo
(现在许多浏览器已经不存在延迟问题了,详见fastclick github,但笔者的手机浏览器还是出现了三百毫秒延迟的问题)
截图如下

为什么会300ms延迟呢,主要是有一个双击缩放功能,浏览器需要判断用户点击是否为双击缩放。这个问题不解决,
1、用户体验就会很差,很不流畅,尤其是在密集操作场景下,比如计算器,不解决300ms延迟问题,感觉反应很慢;
2、点击穿透问题

事件触发顺序

在了解fastclick的思路之前,我们先看一下事件触发顺序是怎样的

touchstart

touchmove

touchend

mouseover :当指针设备移动到存在监听器的元素或其子元素的时候,mouseover事件就会被触发。

mouseenter:当指针设备( 通常指鼠标 )在元素上移动时, mousemove 事件被触发。

mousedown

click

移动端click有300ms延迟问题,touch可没有哦。

fastclick思路

fastclick的思路就是利用touch来模拟tap(触碰),如果认为是一次有效的tap,则在touchend时立即模拟一个click事件,分发到事件源(相当于主动触发一次click),同时阻止掉浏览器300ms后产生的click。

源码学习

先看使用示例,很简单,我们的思路就一直跟着attach走。

</>复制代码

  1. if ("addEventListener" in document) {
  2. document.addEventListener("DOMContentLoaded", function() {
  3. FastClick.attach(document.body);
  4. }, false);
  5. }

直接给body绑定fastlick就行了- -。
看源代码结构(注:以下所有代码均去掉了一些不影响理解思路的部分,大部分思路写在注释中)

</>复制代码

  1. //构造函数
  2. function FastClick(layer, options)
  3. //判断是否需要浏览器原生的click事件(针对一些特殊元素比如表单)
  4. FastClick.prototype.needsClick = function(target)
  5. //发送模拟的click event
  6. FastClick.prototype.sendClick = function(targetElement, event)
  7. // touchstart eventhandler
  8. FastClick.prototype.onTouchStart = function(event)
  9. // touchmove eventhandler
  10. FastClick.prototype.onTouchMove = function(event)
  11. // touchend eventhandler
  12. FastClick.prototype.onTouchEnd = function(event)
  13. // 判断这次tap是否有效
  14. FastClick.prototype.onMouse = function(event)
  15. //click handler 捕获阶段监听
  16. FastClick.prototype.onClick = function(event)
  17. //销毁fastlick,移除事件绑定
  18. FastClick.prototype.destroy = function()
  19. //绑定接口
  20. FastClick.attach = function(layer, options) {
  21. return new FastClick(layer, options);
  22. };

attach实际就执行了构造函数进行初始化,接下来我们来看构造函数发生了什么

</>复制代码

  1. function FastClick(layer,options){
  2. //一些属性初始化
  3. //安卓一些老版本浏览器不支持bind, poly fill
  4. function bind (method, context) {
  5. return function () {
  6. return method.apply(context, arguments);
  7. };
  8. }
  9. var methods = ["onMouse", "onClick", "onTouchStart", "onTouchMove",
  10. "onTouchEnd", "onTouchCancel"];
  11. var context = this;
  12. //将所有handler的this绑定到fastclick实例
  13. for (var i = 0, l = methods.length; i < l; i++) {
  14. context[methods[i]] = bind(context[methods[i]], context);
  15. }
  16. //为当前fast click对象绑定的layer(我们的示例中时document.body)加监听
  17. layer.addEventListener("click", this.onClick, true);//true 捕获阶段触发
  18. layer.addEventListener("touchstart", this.onTouchStart, false);
  19. layer.addEventListener("touchmove", this.onTouchMove, false);
  20. layer.addEventListener("touchend", this.onTouchEnd, false);
  21. layer.addEventListener("touchcancel", this.onTouchCancel, false);
  22. }

构造函数主要是初始化一些属性,polyfill,和添加监听,
下面开始看一下重头戏,touchstart,touchend是如何判断tap是否有效、如何模拟click事件、如何阻止300ms后的click
touchstart

</>复制代码

  1. FastClick.prototype.onTouchStart = function (event) {
  2. var targetElement, touch, selection;
  3. // Ignore multiple touches, otherwise pinch-to-zoom is prevented if both fingers are on the FastClick element (issue #111).
  4. // 如果多触点可能是在缩放,不对targetElement初始化,在此提前终止避免误模拟产生click
  5. if (event.targetTouches.length > 1) {
  6. return true;
  7. }
  8. //获取发生事件源元素(目标阶段的元素)
  9. targetElement = this.getTargetElementFromEventTarget(event.target);
  10. touch = event.targetTouches[0];
  11. this.trackingClick = true;//标记开始跟踪click
  12. this.trackingClickStart = event.timeStamp;//开始跟踪时间
  13. this.targetElement = targetElement;//事件源元素
  14. //触摸坐标,接下来判断是否越界用到
  15. this.touchStartX = touch.pageX;
  16. this.touchStartY = touch.pageY;
  17. // Prevent phantom clicks on fast double-tap (issue #36)
  18. if ((event.timeStamp - this.lastClickTime) < this.tapDelay) {
  19. event.preventDefault();//阻止之后的click
  20. }
  21. return true;
  22. };

touchstart主要是初始化跟踪的tap相关的一些属性,用于之后的判断‘
接下来touchmove

</>复制代码

  1. FastClick.prototype.onTouchMove = function (event) {
  2. if (!this.trackingClick) {
  3. return true;
  4. }
  5. // If the touch has moved, cancel the click tracking 移动到了其他元素
  6. if (this.targetElement !== this.getTargetElementFromEventTarget(event.target) || this.touchHasMoved(event)) {//移动越界了,取消本次click模拟处理,走原生流程
  7. this.trackingClick = false;
  8. this.targetElement = null;
  9. }
  10. return true;
  11. };

touchmove比较简单,主要是兼容滑动tap(swiper)等等,滑动越界则不模拟click
下面是touchend

</>复制代码

  1. FastClick.prototype.onTouchEnd = function (event) {
  2. var forElement, trackingClickStart, targetTagName, scrollParent, touch, targetElement = this.targetElement;
  3. if (!this.trackingClick) {
  4. return true;
  5. }
  6. // Prevent phantom clicks on fast double-tap (issue #36)
  7. //阻止快速双击
  8. if ((event.timeStamp - this.lastClickTime) < this.tapDelay) {
  9. this.cancelNextClick = true;
  10. return true;
  11. }
  12. //超时就不算click了,走原生流程,不阻止click
  13. if ((event.timeStamp - this.trackingClickStart) > this.tapTimeout) {
  14. return true;
  15. }
  16. this.lastClickTime = event.timeStamp;
  17. this.trackingClick = false;
  18. this.trackingClickStart = 0;
  19. // Prevent the actual click from going though - unless the target node is marked as requiring
  20. // real clicks or if it is in the whitelist in which case only non-programmatic clicks are permitted.
  21. if (!this.needsClick(targetElement)) {
  22. event.preventDefault();//阻止之后的click
  23. this.sendClick(targetElement, event);//发送模拟click
  24. }
  25. return false;
  26. };
  27. //发送模拟的click event
  28. FastClick.prototype.sendClick = function (targetElement, event) {
  29. var clickEvent, touch;
  30. // On some Android devices activeElement needs to be blurred otherwise the synthetic click will have no effect (#24)
  31. if (document.activeElement && document.activeElement !== targetElement) {
  32. document.activeElement.blur();
  33. }
  34. touch = event.changedTouches[0];
  35. //模拟click
  36. // Synthesise a click event, with an extra attribute so it can be tracked
  37. clickEvent = document.createEvent("MouseEvents");
  38. clickEvent.initMouseEvent(this.determineEventType(targetElement), true, true, window, 1, touch.screenX, touch.screenY, touch.clientX, touch.clientY, false, false, false, false, 0, null);
  39. clickEvent.forwardedTouchEvent = true;
  40. //向targetElement分发模拟的click
  41. targetElement.dispatchEvent(clickEvent);
  42. };

最后,还会在layer的click捕获阶段监听

</>复制代码

  1. //click handler 捕获阶段监听
  2. FastClick.prototype.onClick = function (event) {
  3. var permitted;
  4. // It"s possible for another FastClick-like library delivered with third-party code to fire a click event before FastClick does (issue #44). In that case, set the click-tracking flag back to false and return early. This will cause onTouchEnd to return early.
  5. if (this.trackingClick) {//1、出界会置为false2成功模拟了一次完成tap并阻止click也会置为false3、避免三方库影响
  6. this.targetElement = null;
  7. this.trackingClick = false;
  8. return true;
  9. }
  10. // Very odd behaviour on iOS (issue #18): if a submit element is present inside a form and the user hits enter in the iOS simulator or clicks the Go button on the pop-up OS keyboard the a kind of "fake" click event will be triggered with the submit-type input element as the target.
  11. if (event.target.type === "submit" && event.detail === 0) {
  12. return true;
  13. }
  14. permitted = this.onMouse(event);
  15. // Only unset targetElement if the click is not permitted. This will ensure that the check for !targetElement in onMouse fails and the browser"s click doesn"t go through.
  16. if (!permitted) {
  17. this.targetElement = null;
  18. }
  19. // If clicks are permitted, return true for the action to go through.
  20. return permitted;
  21. };
  22. // 判断这次鼠标是否有效
  23. FastClick.prototype.onMouse = function (event) {
  24. // If a target element was never set (because a touch event was never fired) allow the event
  25. if (!this.targetElement) {
  26. return true;
  27. }
  28. // 标记fastclick模拟产生的event
  29. if (event.forwardedTouchEvent) {
  30. return true;
  31. }
  32. // Programmatically generated events targeting a specific element should be permitted
  33. if (!event.cancelable) {
  34. return true;
  35. }
  36. // Derive and check the target element to see whether the mouse event needs to be permitted;
  37. // unless explicitly enabled, prevent non-touch click events from triggering actions,
  38. // to prevent ghost/doubleclicks.
  39. // 是否需要原生的click
  40. if (!this.needsClick(this.targetElement) || this.cancelNextClick) {
  41. // Prevent any user-added listeners declared on FastClick element from being fired.
  42. if (event.stopImmediatePropagation) {
  43. event.stopImmediatePropagation();
  44. } else {
  45. // Part of the hack for browsers that don"t support Event#stopImmediatePropagation (e.g. Android 2)
  46. event.propagationStopped = true;
  47. }
  48. // Cancel the event 阻止事件捕获和冒泡
  49. event.stopPropagation();
  50. event.preventDefault();
  51. return false;
  52. }
  53. // If the mouse event is permitted, return true for the action to go through.
  54. return true;
  55. };

这里主要是判断这次click是否有效(如无效,则阻止捕获和冒泡)
至此基本流程已经结束。
其中有1个注意的点,笔者在chrome(Version 64.0.3282.119 (Official Build) (64-bit))已测试
stopPropagation,stopImmediatePropagation不仅会阻止冒泡还会阻止捕获过程哦。

最后

推荐阅读源码,源码中有许多关于focus、不同浏览器兼容和特殊表单元素的处理fastclick github。
这里是笔者带有中文注释的代码中文注释代码。
如有纰漏,欢迎批评指正。

Reference

MDN
https://juejin.im/entry/55d73...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/52463.html

相关文章

  • 移动触摸点击事件优化fastclick源码学习

    摘要:移动端触摸点击事件优化源码学习最近在做一些微信移动端的页面,在此记录关于移动端触摸和点击事件的学习优化过程,主要内容围绕展开。当指针设备通常指鼠标在元素上移动时事件被触发。移动端有延迟问题,可没有哦。 移动端触摸、点击事件优化(fastclick源码学习) 最近在做一些微信移动端的页面,在此记录关于移动端触摸和点击事件的学习优化过程,主要内容围绕fastclick展开。fastclic...

    paney129 评论0 收藏0
  • 移动使用swiper+iscroll+fastClick:安卓触摸swiper会触发点击事件

    摘要:难道是安卓上和执行顺序异于其他浏览器。因为使用了以后事件变得极其敏感,所有的事件触发之前,都会触发。按照的逻辑,一旦触发之后,所有的都被阻止冒泡,就会出现上面说的问题,解决方案如下图增加上图这个判定的即可。 这两天做H5页面,使用swiper+iscroll+fastClick,并没有用swiper提供的tap和click事件,自己在元素上bind,因为回调函数是统一处理,就没用swi...

    KoreyLee 评论0 收藏0
  • zepto touch 库源码分析

    摘要:源码分析不愿意下代码的可以直接点这里地址首先赞一下的代码注释,非常全。属性一个对象,包含了代表所有从上一次触摸事件到此次事件过程中,状态发生了改变的触点的对象。 所谓 zepto 的 touch 其实就是指这个文件啦,可以看到区区 165 行(包括注释)就完成了 swipe 和 tap 相关的事件实现。在正式开始分析源码之前,我们先说说 touch 相关的几个事件,因为无论是 tap ...

    lentrue 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<