Javascript iphone 的 safari touchmove 事件不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3531997/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:03:09  来源:igfitidea点击:

iphone's safari touchmove event not working

javascriptjqueryiphonesafari

提问by coure2011

i am using jquery and touchmove event but the code is not showing anything in #info

我正在使用 jquery 和 touchmove 事件,但代码在 #info 中没有显示任何内容

$('#movieShow').bind('touchmove',function(e){                   
    e.preventDefault();                 
    $('#info').text(e.touches[0].pageX);
});         

回答by Xavi

Try using e.originalEvent.touches:

尝试使用e.originalEvent.touches

$('#movieShow').bind('touchmove',function(e){
    e.preventDefault();

    var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
    console.log(touch.pageX);
});

I ran into a similar problem when I was playing around with touch events and jquery: http://xavi.co/articles/trouble-with-touch-events-jquery

我在玩触摸事件和 jquery 时遇到了类似的问题:http: //xavi.co/articles/trouble-with-touch-events-jquery

回答by donohoe

It might be as simple as a mis-named DIVid ('#info') but can't tell without seeing everything.

它可能就像一个错误命名的DIVid (' #info')一样简单,但不看一切就无法判断。

Try this, and see if you still get no output:

试试这个,看看你是否仍然没有输出:

$('#movieShow').bind('touchmove',function(e){                   
    e.preventDefault();                 
    console.log(e.touches[0].pageX);
});

(You'll need to turn on Debug Consolein MobileSafari)

(您需要在 MobileSafari 中打开调试控制台

UPDATE

更新

So, from your comment you get an error: 'e.touches' is not an object

所以,从你的评论中你得到一个错误: 'e.touches' is not an object

In that case try this (not jQuery specific):

在这种情况下,试试这个(不是特定于 jQuery 的):

document.addEventListener('touchmove', function(e) { e.preventDefault(); }, false);
document.getElementById('movieShow').addEventListener('touchmove',  function(e){
  console.log(e.touches[0].pageX);
},  false);