如何将触摸事件转换为鼠标拖动事件

发布于 2025-02-13 04:35:20 字数 1930 浏览 1 评论 0原文

在实现触摸设备图像的拖放式磁盘时,我将触摸事件转换为鼠标事件时遇到了问题。事件没有被派遣或不如可比。我使用了此指南并学习了其他答案。

为了最大程度地减少问题,我只会缩小到拖放事件,而无需下降。希望如果解决问题,另一部分也将得到解决。当我用鼠标拖动图像时,我会得到所需的行为 - 幽灵映像和更改的光标:

我需要拥有当设备上的触摸时相同的幽灵图像,但没有运气:

”在此处输入图像描述”

为了实现所需的行为,我尝试发生触摸事件时模拟鼠标事件。

<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>

fiddle

看来,触摸事件似乎没有转换为拖放事件。我该如何实现?如果我将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:

enter image description here

I need to have the same ghostly image when touch-drag on a device, but no luck:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文