如何使用 jquery-mobile 获取点击事件的位置/坐标? [英] How to get position/coordinates of a tap-event with jquery-mobile?

查看:21
本文介绍了如何使用 jquery-mobile 获取点击事件的位置/坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jquery-mobile 支持这些事件:

Jquery-mobile supports these events:

http://jquerymobile.com/demos/1.0a3/docs/api/events.html

如何在移动设备上获取点击事件的位置(例如在图像中)?

How do I get the position of the tap event (for example within an image) on a mobile device?

$('#myimg').bind('tap', function(e){
  var x = ???;
  var y = ???;

  alert([x, y]);
})

推荐答案

为什么你得不到 pageX &pageY 是您获得不包含任何坐标信息的 touchcancel 事件(毕竟它是非触摸).这不是真正的轻敲或点击,而是没有移动(超过阈值)且速度足够快的触摸事件.

The reason why you won't get pageX & pageY is that you get the touchcancel event which does not contain any coordinate information (it's a non-touch, after all). It's not an actual tap or click, it's a touch event that didn't move (over a threshold) and was quick enough.

我在使用 jQuery 的 touchswipe 插件时遇到了同样的问题,我不得不通过在 touchstart 事件上存储坐标并在取消事件上检索它来解决这个问题,以便能够提取坐标.

I encountered the same thing when using the touchswipe plugin for jQuery and had to work around it by storing the coordinate on the touchstart event and retrieving it on the cancel one to be able to extract the coordinates.

您希望将这些保存在开始事件中(jquery mobile 中的vmousedown"?):

You will want to save these ones in the start event ("vmousedown" in jquery mobile?):

  event.touches[0].pageX
  event.touches[0].pageY

此外,在移动设备上,您应该将坐标乘以 window.devicePixelRatio 以获得屏幕上的准确位置.

Also, on a mobile device, you should multiply the coordinates by window.devicePixelRatio to get the accurate position on the screen.

这篇关于如何使用 jquery-mobile 获取点击事件的位置/坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆