2011-12-14 2 views
3

Я хочу получить значение touchstart.pageX, когда событие touchhend уволено, но я не получаю точное значение. Это дает мне значение pageX события touchhend. Что я делаю неправильно?Как получить значение touchstart в событии touchhend?

(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

Вы должны получить значение .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

Нет. Я хочу, чтобы значение touchStart при срабатывании touchhend. – coure2011

Смежные вопросы