如何获取touchend事件中的touchstart值?
我想在触发 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
为什么我在两种方法中没有得到相同的值?我指向同一个变量!
I want to get touchstart.pageX value when touchend event is fired but I am not getting exact value. Its giving me pageX value of touchend event. What I am doing wrong?
(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);
After swipe on element I am getting this in console (swipe from left-right)
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
Whey I am not getting same value in both methods? I am pointing to same variable though!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在结束事件中获取触摸变量的 .target 值。触摸就是从这里开始的。实际上,您甚至不需要 touchStart 变量。最后的触摸事件包含您需要的所有信息。
You should be getting the .target value of the touch variable in your end event. That's where the touch started from. You don't even need the touchStart variable, really. The touch in the end event contains all the information you need.