zepto的tap事件的點透問題的幾種解決方案

zepto的tap事件點透問題分析:git

一、「點透」是什麼

你可能碰到過在列表頁面上建立一個彈出層,彈出層有個關閉的按鈕,你點了這個按鈕關閉彈出層後後,這個按鈕正下方的內容也會執行點擊事件(或打開連接)。這個被定義爲這是一個「點透」現象。github

在前面的項目中遇到了以下圖的問題:在點擊彈出來的選擇組件的右上角完成後會讓完成後面的input輸入框聚焦,彈出輸入鍵盤,也就是點透了ide

二、爲何會出現點透呢?
這個須要從zepto(或者jqm)源碼裏面看關於tap的實現方法:動畫

  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 = 0
101 
102       })
103       // when the browser window loses focus,
104       // for example when a modal dialog is shown,
105       // cancel all ongoing events
106       .on('touchcancel MSPointerCancel pointercancel', cancelAll)
107 
108     // scrolling the window indicates intention of the user
109     // to scroll, not tap or swipe, so cancel all ongoing events
110     $(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   })
View Code

能夠看出zepto的tap經過兼聽綁定在document上的touch事件來完成tap事件的模擬的,及tap事件是冒泡到document上觸發的ui

再點擊完成時的tap事件(touchstart\touchend)須要冒泡到document上纔會觸發,而在冒泡到document以前,用戶手的接觸屏幕(touchstart)和離開屏幕(touchend)是會觸發click事件的,由於click事件有延遲觸發(這就是爲何移動端不用click而用tap的緣由)(大概是300ms,爲了實現safari的雙擊事件的設計),因此在執行完tap事件以後,彈出來的選擇組件立刻就隱藏了,此時click事件還在延遲的300ms之中,當300ms到來的時候,click到的其實不是完成而是隱藏以後的下方的元素,若是正下方的元素綁定的有click事件此時便會觸發,若是沒有綁定click事件的話就當沒click,可是正下方的是input輸入框(或者select選擇框或者單選複選框),點擊默認聚焦而彈出輸入鍵盤,也就出現了上面的點透現象。this

三、點透的解決方法:spa

方案一:來得很直接github上有個fastclick能夠完美解決https://github.com/ftlabs/fastclick設計

引入fastclick.js,由於fastclick源碼不依賴其餘庫因此你能夠在原生的js前直接加上3d

1 window.addEventListener( "load", function() {
2     FastClick.attach( document.body );
3 }, false );

或者有zepto或者jqm的js裏面加上code

1 $(function() {
2     FastClick.attach(document.body);
3 });

固然require的話就這樣:

1 var FastClick = require('fastclick');
2 FastClick.attach(document.body, options);

方案二:用touchend代替tap事件並阻止掉touchend的默認行爲preventDefault()

1 $("#cbFinish").on("touchend", function (event) {
2     //不少處理好比隱藏什麼的
3     event.preventDefault();
4 });

方案三:延遲必定的時間(300ms+)來處理事件

1 $("#cbFinish").on("tap", function (event) {
2     setTimeout(function(){
3     //不少處理好比隱藏什麼的
4     },320);
5 });    

這種方法其實很好,能夠和fadeInIn/fadeOut等動畫結合使用,能夠作出過分效果

理論上上面的方法能夠完美的解決tap的點透問題,若是真的倔強到不行,用click

相關文章
相關標籤/搜索