前端开发之tap点透浅谈

zepto的tap事件点透问题分析:

一、点透是什么

你可能碰到过在列表页面上创建一个弹出层,弹出层有个关闭的按钮,你点了这个按钮关闭弹出层后后,这个按钮正下方的内容也会执行点击事件(或打开链接)。这个被定义为这是一个“点透”现象。
在前面的项目中遇到了如下图的问题:在点击弹出来的选择组件的右上角完成后会让完成后面的input输入框聚焦,弹出输入键盘,也就是点透了


技术分享

二、为什么会出现点透?

我们来看看zepto源码里面看关于tap的实现方法:

(http://upload-images.jianshu.io/upload_images/1782900-f64f9813022596c6.gif?imageMogr2/auto-orient/strip) 1 $(document).ready(function(){ 2 var now, delta, deltaX = 0, deltaY = 0, firstTouch, _isPointerType 3 4 if (‘MSGesture‘ in window) { 5 gesture = new MSGesture() 6 gesture.target = document.body 7 } 8 9 $(document) 10 .bind(‘MSGestureEnd‘, function(e){ 11 var swipeDirectionFromVelocity = 12 e.velocityX > 1 ? ‘Right‘ : e.velocityX < -1 ? ‘Left‘ : e.velocityY > 1 ? ‘Down‘ : e.velocityY < -1 ? ‘Up‘ : null; 13 if (swipeDirectionFromVelocity) { 14 touch.el.trigger(‘swipe‘) 15 touch.el.trigger(‘swipe‘+ swipeDirectionFromVelocity) 16 } 17 }) 18 .on(‘touchstart MSPointerDown pointerdown‘, function(e){ 19 if((_isPointerType = isPointerEventType(e, ‘down‘)) && 20 !isPrimaryTouch(e)) return 21 firstTouch = _isPointerType ? e : e.touches[0] 22 if (e.touches && e.touches.length === 1 && touch.x2) { 23 // Clear out touch movement data if we have it sticking around 24 // This can occur if touchcancel doesn‘t fire due to preventDefault, etc. 25 touch.x2 = undefined 26 touch.y2 = undefined 27 } 28 now = Date.now() 29 delta = now - (touch.last || now) 30 touch.el = $(‘tagName‘ in firstTouch.target ? 31 firstTouch.target : firstTouch.target.parentNode) 32 touchTimeout && clearTimeout(touchTimeout) 33 touch.x1 = firstTouch.pageX 34 touch.y1 = firstTouch.pageY 35 if (delta > 0 && delta <= 250) touch.isDoubleTap = true 36 touch.last = now 37 longTapTimeout = setTimeout(longTap, longTapDelay) 38 // adds the current touch contact for IE gesture recognition 39 if (gesture && _isPointerType) gesture.addPointer(e.pointerId); 40 }) 41 .on(‘touchmove MSPointerMove pointermove‘, function(e){ 42 if((_isPointerType = isPointerEventType(e, ‘move‘)) && 43 !isPrimaryTouch(e)) return 44 firstTouch = _isPointerType ? e : e.touches[0] 45 cancelLongTap() 46 touch.x2 = firstTouch.pageX 47 touch.y2 = firstTouch.pageY 48 49 deltaX += Math.abs(touch.x1 - touch.x2) 50 deltaY += Math.abs(touch.y1 - touch.y2) 51 }) 52 .on(‘touchend MSPointerUp pointerup‘, function(e){ 53 if((_isPointerType = isPointerEventType(e, ‘up‘)) && 54 !isPrimaryTouch(e)) return 55 cancelLongTap() 56 57 // swipe 58 if ((touch.x2 && Math.abs(touch.x1 - touch.x2) > 30) || 59 (touch.y2 && Math.abs(touch.y1 - touch.y2) > 30)) 60 61 swipeTimeout = setTimeout(function() { 62 touch.el.trigger(‘swipe‘) 63 touch.el.trigger(‘swipe‘ + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2))) 64 touch = {} 65 }, 0) 66 67 // normal tap 68 else if (‘last‘ in touch) 69 // don‘t fire tap when delta position changed by more than 30 pixels, 70 // for instance when moving to a point and back to origin 71 if (deltaX < 30 && deltaY < 30) { 72 // delay by one tick so we can cancel the ‘tap‘ event if ‘scroll‘ fires 73 // (‘tap‘ fires before ‘scroll‘) 74 tapTimeout = setTimeout(function() { 75 76 // trigger universal ‘tap‘ with the option to cancelTouch() 77 // (cancelTouch cancels processing of single vs double taps for faster ‘tap‘ response) 78 var event = $.Event(‘tap‘) 79 event.cancelTouch = cancelAll 80 touch.el.trigger(event) 81 82 // trigger double tap immediately 83 if (touch.isDoubleTap) { 84 if (touch.el) touch.el.trigger(‘doubleTap‘) 85 touch = {} 86 } 87 88 // trigger single tap after 250ms of inactivity 89 else { 90 touchTimeout = setTimeout(function(){ 91 touchTimeout = null 92 if (touch.el) touch.el.trigger(‘singleTap‘) 93 touch = {} 94 }, 250) 95 } 96 }, 0) 97 } else { 98 touch = {} 99 }100 deltaX = deltaY = 0101 102 })103 // when the browser window loses focus,104 // for example when a modal dialog is shown,105 // cancel all ongoing events106 .on(‘touchcancel MSPointerCancel pointercancel‘, cancelAll)107 108 // scrolling the window indicates intention of the user109 // to scroll, not tap or swipe, so cancel all ongoing events110 $(window).on(‘scroll‘, cancelAll)111 })112 113 ;[‘swipe‘, ‘swipeLeft‘, ‘swipeRight‘, ‘swipeUp‘, ‘swipeDown‘,114 ‘doubleTap‘, ‘tap‘, ‘singleTap‘, ‘longTap‘].forEach(function(eventName){115 $.fn[eventName] = function(callback){ return this.on(eventName, callback) }116 })

可以看出zepto的tap通过兼听绑定在document上的touch事件来完成tap事件的模拟的,及tap事件是冒泡到document上触发的。
再点击完成时的tap事件(touchstart、touchend)需要冒泡到document上才会触发,而在冒泡到document之前,用户手的接触屏幕(touchstart)和离开屏幕(touchend)是会触发click事件的,因为click事件有延迟触发(这就是为什么移动端不用click而用tap的原因。大概是300ms,为了实现safari的双击事件的设计),所以在执行完tap事件之后,弹出来的选择组件马上就隐藏了,此时click事件还在延迟的300ms之中,当300ms到来的时候,click到的其实不是完成而是隐藏之后的下方的元素,如果正下方的元素绑定的有click事件此时便会触发,如果没有绑定click事件的话就当没click,但是正下方的是input输入框(或者select选择框或者单选复选框),点击默认聚焦而弹出输入键盘,也就出现了上面的点透现象。

三、点透的解决方法:

  • 方案一:引入fastclick
    github链接:https://github.com/ftlabs/fastclick
    引入fastclick.js,因为fastclick源码不依赖其他库,所以你可以在原生的js前直接加上
window.addEventListener( "load", function() {
  FastClick.attach(document.body);
}, false );

或者有zepto或者jqm的js里面加上

$(function() {
  FastClick.attach(document.body);
});
  • 方案二:用touchend代替tap事件,并阻止touchend默认行为preventDefault()
$("#cbFinish").on("touchend", function (event) { // 很多处理比如隐藏什么的
  event.preventDefault();
});
  • 方案三:延迟一定的时间(300ms+)来处理事件
$("#cbFinish").on("tap", function (event) {
  setTimeout(function(){
    //一些处理
  },320);
});

这种方法其实很好,可以和fadeInIn/fadeOut等动画结合使用,可以做出过度效果。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容