Javascript touchend 事件属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7192563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
touchend event properties
提问by Matt Roberts
If I catch all touchend events from mobile devices with:
如果我通过以下方式从移动设备捕获所有 touchend 事件:
$(document.body).bind('touchend', function (e) {
var touch = e.touches[0]; // doesnt work
...
I need to get the touch.screenX, touch.screenY, touch.clientX and touch.clientX from the e paramter. All the examples I've seen suggest that e.touches will be a collection, and you can get at the touch details with e.touches[0]
. In my tests on an ipad, e.touches
is always undefined. I'm not using any jquery plugins.
我需要从 e 参数中获取 touch.screenX、touch.screenY、touch.clientX 和 touch.clientX。我见过的所有示例都表明 e.touches 将是一个集合,您可以使用e.touches[0]
. 在我对 ipad 的测试中,e.touches
始终未定义。我没有使用任何 jquery 插件。
Also tried e.targetTouches, which is also undefined.
还尝试了 e.targetTouches,它也是未定义的。
Can anyone help?
任何人都可以帮忙吗?
回答by Gopherkhan
Actually, released touches will be found in the changedTouches array, ie:
实际上,释放的触摸会在 changedTouches 数组中找到,即:
e.changedTouches[0].pageX // get the end x page coordinate for a released touch
e.changedTouches[0].pageX // get the end x page coordinate for a released touch
I think this is slightly more reliable than going through the originalEvent property.
我认为这比通过 originalEvent 属性稍微更可靠。
You can read more on changedTouches here: http://www.w3.org/TR/touch-events/#changedtouches-of-a-touchevent
您可以在此处阅读有关 changedTouches 的更多信息:http: //www.w3.org/TR/touch-events/#changedtouches-of-a-touchevent
回答by javibilbo
The touches property is an TouchList object. You can see the TouchList class reference here.
touches 属性是一个 TouchList 对象。您可以在此处查看 TouchList 类参考。
If you monitor its length property with this example code on #log div:
如果您在 #log div 上使用此示例代码监视其长度属性:
$('#element').bind('touchstart', function(event)
{
$('#log').append(event.originalEvent.touches.length+'<br/>');
});
$('#element').bind('touchmove', function(event)
{
$('#log').append(event.originalEvent.touches.length+'<br/>');
});
$('#element').bind('touchend', function(event)
{
$('#log').append(event.originalEvent.touches.length+'<br/>');
});
you will obtain 1 while executing touchstart and touchmove, an 0 when executing touchend. That is why you obtain undefined from e.touches[0]
.
执行 touchstart 和 touchmove 时将获得 1,执行 touchend 时将获得 0。这就是为什么您从e.touches[0]
.