javascript 使用 jquery.hammer.js 时如何返回 X 和 Y 坐标

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

How to return X and Y coordinates when using jquery.hammer.js

javascriptjquerycoordinateshammer.js

提问by DFania

I am programming a little page using jquery.hammer.js and I have having problems getting the x and y coordinates of a touch event.

我正在使用 jquery.hammer.js 编写一个小页面,但在获取触摸事件的 x 和 y 坐标时遇到问题。

my code is as follows:

我的代码如下:

var x1, y1;
var element = document.getElementById("myDIV")
$(element).hammer().on("touch", function(e){
x1 = e.clientX
y1 = e.clientY

document.getElementById("resultDIV").innerHTML = x1 + " " + y1
}

but all I get is "undefined undefined" as my result.

但我得到的结果是“未定义未定义”。

i have everything i can think of, any help is much appreciated.

我有我能想到的一切,非常感谢任何帮助。

回答by hudidit

e.gestureis deprecated now. With the latest version of Hammer.js, you should use

e.gesture现在已弃用。使用最新版本的 Hammer.js,您应该使用

var tapX, tapY;
tapX = e.center.x;
tapY = e.center.y;

回答by Karlen Kishmiryan

  1. You've missed );at the end.
  2. Try to always use semicolons (;) in the end of each statement.
  3. If you're starting to use some library, read the documentation and how-to section first.
  1. 你已经错过);了最后。
  2. 尽量;在每个的末尾使用分号 ( ) statement
  3. 如果您开始使用某个库,请先阅读文档和操作方法部分。

You can find the information you want from the documentation on GitHub.

您可以从GitHub 上文档中找到所需的信息。

Update:
I've analyzed the eventobject and found the gestureproperty of this object. The gesturecontains another property, which is called center. And finally you can get the X and Y coordinates from this property by using pageXand pageY.

更新:
我已经分析了该event对象并找到了gesture该对象的属性。该gesture包含另一个属性,这就是所谓center。最后,您可以使用pageXand从此属性中获取 X 和 Y 坐标pageY

var x1, y1;
x1 = e.gesture.center.pageX;
y1 = e.gesture.center.pageY;

Here is the working demo of your example in JSFiddle.

这是您在JSFiddle中的示例的工作演示。

And also if you use multi-touch, you can get the coordinates of each touch by using the touchesarray of gestureproperty. The other property names remain the same (pageX, pageY).

而且,如果您使用multi-touch,您可以通过使用属性touches数组获取每次触摸的坐标gesture。其他属性名称保持不变 ( pageX, pageY)。

var x1, y1;
x1 = e.gesture.touches[0].pageX;
y1 = e.gesture.touches[0].pageY;

Here is the second demoof your example using touchesarray to get coordinates for only the first touch.

这是您使用数组获取仅第一次触摸的坐标的示例的第二个演示touches

回答by Robert

I needed to look into the original event to get mine working. Here is an example for anyone else with a similar situation.

我需要查看原始事件才能让我的工作正常进行。这是其他有类似情况的人的示例。

$("#id").hammer({ domEvents: true }).on("press", ".elems", function (e) {
    console.log("pressed");
    console.log(e.originalEvent.gesture.center.x);
    console.log(e.originalEvent.gesture.center.y);
    return false;
});

Hammer version Hammer.JS - v2.0.4 - 2014-09-28

锤子版 Hammer.JS - v2.0.4 - 2014-09-28

回答by TmTron

in hammerjs 2.0.8I use ev.srcEvent.offsetXand ev.srcEvent.offsetY

在hammerjs中2.0.8我使用ev.srcEvent.offsetXev.srcEvent.offsetY