如何将触摸事件转换为鼠标拖动事件
在实现触摸设备图像的拖放式磁盘时,我将触摸事件转换为鼠标事件时遇到了问题。事件没有被派遣或不如可比。我使用了此指南并学习了其他答案。
为了最大程度地减少问题,我只会缩小到拖放事件,而无需下降。希望如果解决问题,另一部分也将得到解决。当我用鼠标拖动图像时,我会得到所需的行为 - 幽灵映像和更改的光标:
我需要拥有当设备上的触摸时相同的幽灵图像,但没有运气:
为了实现所需的行为,我尝试发生触摸事件时模拟鼠标事件。
<img id="hammer" src="https://i.sstatic.net/9oV2e.png" />
<script>
var hammer = document.getElementById("hammer");
hammer.addEventListener("touchstart", touchHandler, true);
hammer.addEventListener("touchmove", touchHandler, true);
function touchHandler(event) {
var touches = event.changedTouches,
first = touches[0],
type = "";
switch(event.type) {
case "touchstart": type = "dragstart"; break;
case "touchmove": type = "drag"; break;
default: return;
}
var simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent(type, true, true, window, 1,
first.screenX, first.screenY,
first.clientX, first.clientY, false,
false, false, false, 0/*left*/, first.target);
this.dispatchEvent(simulatedEvent);
event.preventDefault();
}
</script>
看来,触摸事件似乎没有转换为拖放事件。我该如何实现?如果我将EventListeners添加到文档
而不是Hammer
,则会遇到错误。如果我用容器div将hammer 包装,然后将AddEventListener包装到容器中,则也不会成功。如果我将type =“ drag”更改为
type =“ mousemove”
,则没有区别。
On implementing drag-n-drop of an image for touch devices, I have a problem converting touch events to mouse events. Events are not dispatched or don't work as supposed. I used this guide and learnt other answers.
To minimize the question, I'll narrow to the drag event only, without drop. Hopefully if it's solved, the other part will be solved, too. When I drag the image with mouse, I get the desired behavior - the ghostly image and the changed cursor:
I need to have the same ghostly image when touch-drag on a device, but no luck:
In order to achieve the desired behavior, I try to simulate a mouse event when touch event happens.
<img id="hammer" src="https://i.sstatic.net/9oV2e.png" />
<script>
var hammer = document.getElementById("hammer");
hammer.addEventListener("touchstart", touchHandler, true);
hammer.addEventListener("touchmove", touchHandler, true);
function touchHandler(event) {
var touches = event.changedTouches,
first = touches[0],
type = "";
switch(event.type) {
case "touchstart": type = "dragstart"; break;
case "touchmove": type = "drag"; break;
default: return;
}
var simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent(type, true, true, window, 1,
first.screenX, first.screenY,
first.clientX, first.clientY, false,
false, false, false, 0/*left*/, first.target);
this.dispatchEvent(simulatedEvent);
event.preventDefault();
}
</script>
The fiddle
It seems that the touch events are not converted to drag events. How can I achieve it? If I add EventListeners to document
instead of hammer
, I get an error. If I wrap hammer
with a container div, and addEventListener to the container, it gives no success, too. If I change type = "drag"
to type = "mousemove"
, no difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论