本文介绍了如何使用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获取点击事件的位置/坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:13