本文介绍了像鼠标事件一样在触摸时检索相同的offsetX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试获取touch事件的offsetX,Y,它应该与鼠标事件的offsetX相同.为此,我使用了以下代码:

Hello I am trying to get the offsetX,Y of touch event which should be identical to offsetX of mouse event. To do so I have used this code:

 ev.offsetX = ev.targetTouches[0].pageX- canvasName.offsetLeft

我什至尝试将触摸事件模拟为鼠标事件,但是为此,我需要offsetX/Y,它在触摸事件中不可用.有什么方法可以计算touchX的offsetX/Y吗?请帮助

I have even tried to simulate the touch event into mouse event but for that purpose i need the offsetX/Y, which is unavailable in touch event. Is there any way offsetX/Y can be calculated for touch? Please help

推荐答案

我使用此

var rect = e.target.getBoundingClientRect();
var x = e.targetTouches[0].pageX - rect.left;
var y = e.targetTouches[0].pageY - rect.top;

这篇关于像鼠标事件一样在触摸时检索相同的offsetX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 23:27