javascript 获取当前元素上的touchstart坐标

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

get touchstart coordinate on current element

javascripthtml

提问by h4lc0n

So I have an element that looks like this:

所以我有一个看起来像这样的元素:

 ____________________________________
/                                    \
|                                    |
|                                    |
+------------------------------------+
|                                    |
|                                    |
|                                    |
+------------------------------------+
|                                    |
|                                    |
\____________________________________/

I have attached a touchstart listener to it, like this:

我已经为它附加了一个 touchstart 监听器,如下所示:

    other_options.addEventListener('touchstart', function(e) {
        event.preventDefault();
    }, false);

What I want to do, and I've already looked through the values of 'e' but I can't find any value consistent enough (values don't seem right to me when I try them) to do what I want.

我想要做什么,并且我已经查看了 'e' 的值,但是我找不到任何足够一致的值(当我尝试它们时,这些值对我来说似乎不正确)来做我想做的事。

I know the size of those rows. I just want to be able to take the Y coordinate of where the touchstart event fired, being 0 the upper coordinate of the element. That way, Math.floor(y / ROW_SIZE) will give me the row the touchstart event was started on.

我知道这些行的大小。我只是希望能够获取触发 touchstart 事件的 Y 坐标,将元素的上坐标设为 0。这样, Math.floor(y / ROW_SIZE) 会给我开始 touchstart 事件的行。

回答by BalaKrishnan?

You have to access like this.

你必须像这样访问。

e.touches[0].clientX/clientY.

the number touchs and touch information can be access via e.touches.

触摸次数和触摸信息可以通过 e.touches 访问。

Take look at this FAQ.

看看这个常见问题。

How to get the x/y coordinates in touch events ?

如何在触摸事件中获取 x/y 坐标?

here is the touch event documentation.

这是触摸事件文档

By the way, events will return x/y coordinates relative to the window only. we can't changed that. what you can do this. top= 0 - element.top; x= clientx-top.

顺便说一下,事件将仅返回相对于窗口的 x/y 坐标。我们不能改变这一点。你能做什么。top= 0 - element.top; x= clientx-top.

回答by Venkat

It is better to use getBoundingClientRect()method to calculate top & left values of an element.

最好使用getBoundingClientRect()方法来计算元素的顶部和左侧值。

The above approach, element.top, may not give top & left values relative to the viewport if the element is inside another container.

element.top如果元素位于另一个容器内,则上述方法可能不会给出相对于视口的顶部和左侧值。

So to calculate touch co-ordinates relative to the element, we can do as below.

因此,要计算相对于元素的触摸坐标,我们可以执行以下操作。

var rect = element.getBoundingClientRect();
xCoordinate = event.touches[0].clientX - rect.left;
yCoordinate = event.touches[0].clientY - rect.top;

To find out more about getBoundingClientRect()method, click here.

要了解有关getBoundingClientRect()方法的更多信息,请单击此处