我想在触发 touchend 事件时获取 touchstart.pageX 值,但我没有获得确切的值。它给了我 touchend 事件的 pageX 值。我做错了什么?
(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
为什么我在这两种方法中没有得到相同的值?不过我指的是同一个变量!!
最佳答案
您应该在结束事件中获取 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
关于javascript - 如何在 touchend 事件中获取 touchstart 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8499735/