如何在Safari for iPad中使用jQuery识别触摸事件?可能吗?

Abh*_* B. 188 jquery events mobile-safari javascript-events ipad

是否可以使用jQuery在iPad的Safari浏览器上识别触摸事件?

我在Web应用程序中使用了mouseOver和mouseOut事件.iPad的Safari浏览器是否有类似的事件,因为没有像mouseOut,mouseMove这样的事件?

Dav*_*ean 236

核心jQuery对触摸事件没有任何特殊之处,但您可以使用以下事件轻松构建自己的事件

  • touchstart
  • touchmove
  • touchend
  • touchcancel

例如, touchmove

document.addEventListener('touchmove', function(e) {
    e.preventDefault();
    var touch = e.touches[0];
    alert(touch.pageX + " - " + touch.pageY);
}, false);
Run Code Online (Sandbox Code Playgroud)

这适用于大多数基于webkit的浏览器(包括android).

这是一些很好的文件:

  • e是自动传递给处理程序的事件对象.对于safari浏览器(以及android也是如此),它现在包含用户在屏幕上进行的所有触摸的数组.每次触摸都有自己的属性(例如x,y coords) (7认同)
  • 非常感谢大卫,我认为它会起作用,但是什么是e.touches [0]?你能详细描述一下"e"这个论点吗? (2认同)
  • 好吧,我知道你会使用像e.target这样的东西 (2认同)

Tim*_*rez 146

如果您使用的是jQuery 1.7+,它甚至比其他所有答案都简单.

$('#whatever').on({ 'touchstart' : function(){ /* do something... */ } });
Run Code Online (Sandbox Code Playgroud)

  • @Octopus:你会在`ev.originalEvent`下找到触摸信息. (8认同)
  • 这很好用.我需要click和touchstart做同样的事情,我习惯了非对象语法,如`$('#whatever').on('touchstart click',function(){/*做点什么.. .*/});` (6认同)

Dav*_*one 24

最简单的方法是使用像Hammer.js这样多点触控JavaScript库.然后你可以编写如下代码:

canvas
    .hammer({prevent_default: true})
    .bind('doubletap', function(e) { // And double click
        // Zoom-in
    })
    .bind('dragstart', function(e) { // And mousedown
        // Get ready to drag
    })
    .bind('drag', function(e) { // And mousemove when mousedown
        // Pan the image
    })
    .bind('dragend', function(e) { // And mouseup
        // Finish the drag
    });
Run Code Online (Sandbox Code Playgroud)

你可以坚持下去.它支持点击,双击,滑动,按住,变换(即捏合)和拖动.当发生等效的鼠标操作时,触摸事件也会触发,因此您不需要编写两组事件处理程序.哦,你需要jQuery插件,如果你想像我一样以jQueryish方式编写.

  • "最简单"的解决方案从不涉及通过添加库使问题更复杂 (29认同)
  • @Octopus最简单的解决方案是使用hammer.js来抽象出跨浏览器的头痛.使用jQuery扩展名在您熟悉的区域中.对我来说似乎并不太复杂. (13认同)

Ima*_*ghi 24

单独使用touchstart或touchend不是一个好的解决方案,因为如果您滚动页面,设备会将其检测为触摸或点击.因此,同时检测点击和点击事件的最佳方法是仅检测不移动屏幕的触摸事件(滚动).所以要这样做只需将此代码添加到您的应用程序:

$(document).on('touchstart', function() {
    detectTap = true; //detects all touch events
});
$(document).on('touchmove', function() {
    detectTap = false; //Excludes the scroll events from touch events
});
$(document).on('click touchend', function(event) {
    if (event.type == "click") detectTap = true; //detects click events 
       if (detectTap){
          //here you can write the function or codes you wanna execute on tap

       }
 });
Run Code Online (Sandbox Code Playgroud)

我测试了它,它在iPad和iPhone上运行正常.它可以检测水龙头,轻松区分水龙头和触摸滚动.

  • 这个解决方案似乎是我发现的最直接的,似乎在iOS上运行得很好. (2认同)

fea*_*lly 18

您可以使用.on()捕获多个事件,然后测试触摸屏,例如:

$('#selector')
.on('touchstart mousedown', function(e){
  e.preventDefault();
  var touch = e.touches[0];
  if(touch){
    // Do some stuff
  }
  else {
    // Do some other stuff
  }
});
Run Code Online (Sandbox Code Playgroud)


Kar*_*rol 15

jQuery Core没有什么特别之处,但你可以在jQuery Mobile Events页面上阅读有关不同触摸事件的内容,这些内容也可以在iOS设备之外使用.

他们是:

  • 龙头
  • taphold
  • 刷卡
  • swipeleft
  • swiperight

另请注意,在滚动事件期间(基于移动设备上的触摸),iOS设备会在滚动时冻结DOM操作.


小智 6

我有点担心只为我的项目使用touchmove,因为当你的触摸从一个位置移动到另一个位置时(而不是在初始触摸时)它似乎只会触发.因此我将它与touchstart结合起来,这似乎对初始触摸和任何动作都非常有效.

<script>

function doTouch(e) {
    e.preventDefault();
    var touch = e.touches[0];

    document.getElementById("xtext").innerHTML = touch.pageX;
    document.getElementById("ytext").innerHTML = touch.pageY;   
}

document.addEventListener('touchstart', function(e) {doTouch(e);}, false);
document.addEventListener('touchmove', function(e) {doTouch(e);}, false);

</script>

X: <div id="xtext">0</div>
Y: <div id="ytext">0</div>
Run Code Online (Sandbox Code Playgroud)