首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将javascript鼠标事件转换为jquery移动触摸事件?

问如何将javascript鼠标事件转换为jquery移动触摸事件?
EN

Stack Overflow用户
提问于 2020-10-28 01:54:59
回答 1查看 28关注 0票数 0

我是一个移动web应用的新手。我试着为一个网页创建一些鼠标可拖动的div。它们在台式机上工作得很好。然而,当我在移动设备上检查它们时,它们都不起作用。我在网上搜索了帮助,但我得到的唯一信息是我需要用jquery mobile设置触摸事件。一些文章说了一些关于使用touchstart,touchmove和touchend的东西,但没有提供任何例子。

下面是我的脚本:

代码语言:javascript
复制
dragElement(document.getElementById("cn"));
dragElement(document.getElementById("ln"));
dragElement(document.getElementById("gn"));
dragElement(document.getElementById("sn"));

function dragElement(elmnt) {
    var pos1 = 0,
        pos2 = 0,
        pos3 = 0,
        pos4 = 0;
    if (document.getElementById(elmnt.id + "header")) {
        // if present, the header is where you move the DIV from:
        document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
    } else {
        // otherwise, move the DIV from anywhere inside the DIV:
        elmnt.onmousedown = dragMouseDown;
    }
    function dragMouseDown(e) {
        e = e || window.event;
        e.preventDefault();
        // get the mouse cursor position at startup:
        pos3 = e.clientX;
        pos4 = e.clientY;
        document.onmouseup = closeDragElement;
        // call a function whenever the cursor moves:
        document.onmousemove = elementDrag;
    }
    function elementDrag(e) {
        e = e || window.event;
        e.preventDefault();
        // calculate the new cursor position:
        pos1 = pos3 - e.clientX;
        pos2 = pos4 - e.clientY;
        pos3 = e.clientX;
        pos4 = e.clientY;
        // set the element's new position:
        elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
        elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
    }
    function closeDragElement() {
        // stop moving when mouse button is released:
        document.onmouseup = null;
        document.onmousemove = null;
    }
}

我已经尝试用相应的触摸事件替换鼠标事件。但它并没有起作用。看起来我必须在另一个框架中重写代码。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-10-28 05:10:55

vmousecancel表示鼠标取消vmousedown vmousemove vmouseout vmouseover vmouseup

更多示例:https://api.jquerymobile.com/category/events/

在使用之前,您必须将jquery.mobile库放入您的工作区。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64560244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档