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

时间:2020-10-27 17:54:59

标签: javascript jquery

我是移动Web应用程序的新手。我试图为网页创建一些鼠标可拖动的div。它们在桌面上运行良好。但是,当我在移动设备上检查它们时,它们都不起作用。我在网上搜索了帮助,但唯一得到的是我需要使用jquery mobile设置触摸事件。一些文章谈到了有关使用touchstart,touchmove和touchend的内容,但从未提供任何示例。

这是我的剧本:

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

我尝试将鼠标事件替换为相应的触摸事件。但这没有用。 看来我必须在另一个框架中重写代码。预先感谢。

1 个答案:

答案 0 :(得分:0)

vmousecancel意味着取消鼠标 vmousedown vmousemove vmouseout 虚拟鼠标 vmouseup

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

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