2011-12-14 45 views
3

我想在touchend事件觸發時獲取touchstart.pageX值,但我沒有得到確切的值。它給了我touchend事件的pageX值。我做錯了什麼?如何在touchend事件中獲得touchstart價值?

(function($){ 

    $.fn.extend({ 

     //pass the options variable to the function 
     swipeTest: function(options) { 
      var touchStart;    
      var options = $.extend(defaults, options); 

      //initilaized objects 
      function init(thisObj){ 
       thisObj.addEventListener('touchstart', function(e) { 
        var touch = e.touches[0] || e.changedTouches[0]; 
        touchStart = touch; 
        console.log('Value of touchStart.pageX in touchstart method: ' + touchStart.pageX); 
       }, false); 

       thisObj.addEventListener('touchend', function(e) { 
        var touch = e.touches[0] || e.changedTouches[0]; 
        console.log('Value of touchStart.pageX in touchend method: ' + touchStart.pageX); 
       }, false); 
      } 

      return this.each(function() { 
       init(this); 

      }); 
     } 
    }); 

})(jQuery); 

元素上刷卡後,我得到這個控制檯(自左向右掃)

Value of touchStart.pageX in touchstart method: 132 
Value of touchStart.pageX in touchend method: 417 
Value of touchStart.pageX in touchstart method: 32 
Value of touchStart.pageX in touchend method: 481 

乳清我不是在這兩種方法獲得相同的值?我指向同一個變量!

回答

1

您應該在您的結束事件中獲取touch變量的.target值。這就是觸摸的起點。你甚至不需要touchStart變量。結束事件中的觸摸包含您需要的所有信息。

//in your touchend handler, after touch = blah blah blah 
var startnode = touch.target; //gets you the starting node of the touch 
var x = startnode.pageX 
var y = startnode.pageY 
+0

不,我不會對這些感到困惑。問題是,爲什麼touchStart.pageX給touchEnd事件的價值.. – coure2011 2011-12-14 05:53:34