如何在touchend事件中获取touchstart值
how to get touchstart value in touchend event?
我想接触一下。当触摸端事件被触发时,pageX值,但我没有得到确切的值。它给了我触摸端事件的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
相关文章:
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- jquery试图按名称获取按钮位置
- 如何在jQuery中获取元素的形式
- 如何在php文件中获取$.post-ajax传递的值
- 在Shopify中获取博客文章的图片
- 使用Javascript获取所选选项ID
- 在jQuery中获取表的行索引
- 使用jquery将mysql数据获取到新的表行中
- 在动态创建的元素上获取对特定选择器的引用
- 从城市名称获取惊喜
- Angular只从数组中获取所需的数据
- 无法将数据从firebase获取到我的html页面
- 从ajax请求中获取javascript对象
- 如何从画布上的某个移动事件中获取X和Y
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 如何在PHP中使用$_POST获取Select元素值
- 获取@ResponseBody的一部分作为主干和Spring MVC控制器之间的参数
- 如何在touchend事件中获取touchstart值
- 从touchstart事件javascript获取图像ID
- 获取 TouchStart 事件中的元素位置