zepto的touch模塊解決click延遲300ms問題以及點透問題的詳解

你們都知道移動端的click事件會延遲300ms觸發,這時你們可使用zepto的touch模塊,裏面定義了一個tap事件,經過綁定tap事件,能夠實現點擊當即觸發的功能。javascript

那麼,它的tap事件是怎麼實現的呢?這是咱們要解決的第一個問題。java

第二個問題,你們都知道zepto的tap事件會有點透的問題,那麼,點透如何出現,點透爲何會出現,點透問題如何解決等,這是咱們要解決的第二個問題。函數

咱們先來看tap事件是如何實現的?

查看touch.js代碼,在最後的代碼中有如下代碼:this

;['swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown',
    'doubleTap', 'tap', 'singleTap', 'longTap'].forEach(function(eventName){
    $.fn[eventName] = function(callback){ return this.on(eventName, callback) }
  })

上面的代碼,是把tap函數,賦給zepto的原型對象。所以,使用tap事件處理函數,有如下兩種方法:spa

1.$("div").tap(function(){})插件

2.$("div").on("tap",function(){})  orm

接下來,咱們來查看,touch.js是如何實現tap自定義事件的。對象

$(document)
      .bind('MSGestureEnd', function(e){
        var swipeDirectionFromVelocity =
          e.velocityX > 1 ? 'Right' : e.velocityX < -1 ? 'Left' : e.velocityY > 1 ? 'Down' : e.velocityY < -1 ? 'Up' : null;
        if (swipeDirectionFromVelocity) {
          touch.el.trigger('swipe')
          touch.el.trigger('swipe'+ swipeDirectionFromVelocity)
        }
      })
      .on('touchstart MSPointerDown pointerdown', function(e){
        if((_isPointerType = isPointerEventType(e, 'down')) &&
          !isPrimaryTouch(e)) return
        firstTouch = _isPointerType ? e : e.touches[0]
        if (e.touches && e.touches.length === 1 && touch.x2) {
          // Clear out touch movement data if we have it sticking around
          // This can occur if touchcancel doesn't fire due to preventDefault, etc.
          touch.x2 = undefined
          touch.y2 = undefined
        }
        now = Date.now()
        delta = now - (touch.last || now)
        touch.el = $('tagName' in firstTouch.target ?
          firstTouch.target : firstTouch.target.parentNode)
        touchTimeout && clearTimeout(touchTimeout)
        touch.x1 = firstTouch.pageX
        touch.y1 = firstTouch.pageY
        if (delta > 0 && delta <= 250) touch.isDoubleTap = true
        touch.last = now
        longTapTimeout = setTimeout(longTap, longTapDelay)
        // adds the current touch contact for IE gesture recognition
        if (gesture && _isPointerType) gesture.addPointer(e.pointerId);
      })
      .on('touchmove MSPointerMove pointermove', function(e){
        if((_isPointerType = isPointerEventType(e, 'move')) &&
          !isPrimaryTouch(e)) return
        firstTouch = _isPointerType ? e : e.touches[0]
        cancelLongTap()
        touch.x2 = firstTouch.pageX
        touch.y2 = firstTouch.pageY

        deltaX += Math.abs(touch.x1 - touch.x2)
        deltaY += Math.abs(touch.y1 - touch.y2)
      })
      .on('touchend MSPointerUp pointerup', function(e){
        if((_isPointerType = isPointerEventType(e, 'up')) &&
          !isPrimaryTouch(e)) return
        cancelLongTap()

        // swipe
        if ((touch.x2 && Math.abs(touch.x1 - touch.x2) > 30) ||
            (touch.y2 && Math.abs(touch.y1 - touch.y2) > 30))

          swipeTimeout = setTimeout(function() {
            touch.el.trigger('swipe')
            touch.el.trigger('swipe' + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)))
            touch = {}
          }, 0)

        // normal tap
        else if ('last' in touch)
          // don't fire tap when delta position changed by more than 30 pixels,
          // for instance when moving to a point and back to origin
          if (deltaX < 30 && deltaY < 30) {
            // delay by one tick so we can cancel the 'tap' event if 'scroll' fires
            // ('tap' fires before 'scroll')
            tapTimeout = setTimeout(function() {

              // trigger universal 'tap' with the option to cancelTouch()
              // (cancelTouch cancels processing of single vs double taps for faster 'tap' response)
              var event = $.Event('tap')
              event.cancelTouch = cancelAll
              touch.el.trigger(event)

              // trigger double tap immediately
              if (touch.isDoubleTap) {
                if (touch.el) touch.el.trigger('doubleTap')
                touch = {}
              }

              // trigger single tap after 250ms of inactivity
              else {
                touchTimeout = setTimeout(function(){
                  touchTimeout = null
                  if (touch.el) touch.el.trigger('singleTap')
                  touch = {}
                }, 250)
              }
            }, 0)
          } else {
            touch = {}
          }
          deltaX = deltaY = 0

})

上面的代碼,你們都知道,是在document上綁定了不少事件,其中,咱們只要關注touchstart和touchend 這兩個事件綁定。blog

在touchend的事件處理函數中,有如下代碼:事件

tapTimeout = setTimeout(function() {

              // trigger universal 'tap' with the option to cancelTouch()
              // (cancelTouch cancels processing of single vs double taps for faster 'tap' response)
              var event = $.Event('tap')
              event.cancelTouch = cancelAll
              touch.el.trigger(event)

              // trigger double tap immediately
              if (touch.isDoubleTap) {
                if (touch.el) touch.el.trigger('doubleTap')
                touch = {}
              }

              // trigger single tap after 250ms of inactivity
              else {
                touchTimeout = setTimeout(function(){
                  touchTimeout = null
                  if (touch.el) touch.el.trigger('singleTap')
                  touch = {}
                }, 250)
              }
}, 0)

以上代碼就會當即觸發一個自定義的tap事件。這時綁定的tap事件回調函數就會當即執行。

由於點擊事件,就是touchstart和touchend的組合。

當touchend觸發時,就表明一次點擊結束,所以在touchend的回調函數中,觸發一個自定義的tap事件,至關於觸發了tap事件,所以立刻就會執行,而不會出現click事件延遲300ms。  

你們都知道,使用zepto的tap事件,會出現點透的問題。

咱們先來了解下,什麼叫點透問題?

假如你在列表頁面上建立一個彈出層,彈出層有個關閉的按鈕(綁定了tap事件),你點了這個按鈕關閉彈出層後,這個按鈕正下方的內容也會執行點擊事件(或打開連接)。這個就是一個「點透」現象。

點透出現的緣由是:延遲300ms的click事件觸發了。

zepto的touch模塊中,沒有對這個延遲300ms的click事件取消,或者取消不了。

而fastClick中,會對這個延遲300ms的click事件取消,也就是這個click事件不會觸發。

因此zepto的tap事件(經過touchstart和touchend模擬出來的)有點透問題,而fastClick的click事件(經過touchstart和touchend模擬出來的)沒有。

zepto中,沒有對真實的延遲300ms的click事件作處理,或者作了處理,可是仍是觸發了。而fastClick對真實的延遲300ms的click事件作了處理,不會觸發。

深刻到zepto的touch.js和fastClick的源碼,咱們能夠得知:

zepto的tap事件和fastClick的click事件,源碼差很少。

爲何基本相同的代碼,zepto會點透而fastclick不會呢?

緣由是zepto的代碼裏面有個settimeout,在settimeout裏面執行e.preventDefault()不會生效,所以zepto中的延遲300ms的click事件會觸發,而fastClick不會

解決tap點透問題:

1.使用fastclick,不過你以前寫的tap事件,都要改爲click事件。

2.使用基於zepto的tap插件,此插件,代碼量不大,也不用修改你寫的tap事件,須要的請問我要,謝謝。

 

 

 

 

 

加油! 

相關文章
相關標籤/搜索