2011-12-14 11 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

+0

ごめんなさい。更新された回答をご覧ください。 – Ben

+0

いいえ、私はtouchendが発砲したときにtouchStartの値が必要です。 – coure2011

関連する問題