JavaScript将触摸事件映射到鼠标事件

JavaScript将触摸事件映射到鼠标事件,javascript,javascript-events,touchscreen,Javascript,Javascript Events,Touchscreen,我正在使用YUI滑块,它可以处理鼠标移动事件。我想让它响应touchmove事件(iPhone和Android)。发生touchmove事件时,如何生成鼠标移动事件?我希望只需在顶部添加一些脚本,touchmove事件就可以映射到鼠标移动事件,而不必使用滑块进行任何更改 我相信这就是你想要的: function touchHandler(event) { var touches = event.changedTouches, first = touches[0],

我正在使用YUI滑块,它可以处理鼠标移动事件。我想让它响应touchmove事件(iPhone和Android)。发生touchmove事件时,如何生成鼠标移动事件?我希望只需在顶部添加一些脚本,touchmove事件就可以映射到鼠标移动事件,而不必使用滑块进行任何更改

我相信这就是你想要的:

function touchHandler(event)
{
    var touches = event.changedTouches,
        first = touches[0],
        type = "";
    switch(event.type)
    {
        case "touchstart": type = "mousedown"; break;
        case "touchmove":  type = "mousemove"; break;        
        case "touchend":   type = "mouseup";   break;
        default:           return;
    }

    // initMouseEvent(type, canBubble, cancelable, view, clickCount, 
    //                screenX, screenY, clientX, clientY, ctrlKey, 
    //                altKey, shiftKey, metaKey, button, relatedTarget);

    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*/, null);

    first.target.dispatchEvent(simulatedEvent);
    event.preventDefault();
}

function init() 
{
    document.addEventListener("touchstart", touchHandler, true);
    document.addEventListener("touchmove", touchHandler, true);
    document.addEventListener("touchend", touchHandler, true);
    document.addEventListener("touchcancel", touchHandler, true);    
}
我捕获了触摸事件,然后手动触发我自己的鼠标事件以匹配。尽管该代码并不像现在这样具有特殊的通用性,但适应大多数现有的拖放库,可能还有大多数现有的鼠标事件代码应该很简单。希望这个想法对为iPhone开发web应用程序的人有用

更新:

在发布这篇文章时,我注意到对所有触摸事件调用
preventDefault
,将阻止链接正常工作。调用
preventDefault
的主要原因是为了阻止手机滚动,您只需在
touchmove
回调中调用手机即可。这样做的唯一缺点是iPhone有时会在拖动原点上显示其悬停弹出窗口。如果我发现了防止这种情况发生的方法,我会更新这篇文章

第二次更新:


我发现CSS属性可以关闭标注:
-webkit touch callout

按照@Mickey Shine的答案,提供了触摸事件到单击事件的映射。它是为了将这种支持放到jQuery UI中而构建的,但它提供了信息。

我终于找到了一种方法,使用Mickey的解决方案,而不是他的init函数来实现这一点。在这里使用init函数

function init() {
    document.getElementById('filter_div').addEventListener("touchstart", touchHandler, true);
    document.getElementById('filter_div').addEventListener("touchmove", touchHandler, true);
    document.getElementById('filter_div').addEventListener("touchend", touchHandler, true);
    document.getElementById('filter_div').addEventListener("touchcancel", touchHandler, true);
}
如果您看到“
filter\u div
”是我们拥有图表范围过滤器的图表区域,我们只想在该区域重写触摸控制


谢谢你,米奇。这是一个很好的解决方案。

对于将来回到这里的用户,可以使用以下代码:

var eventMap = {};

(function(){
var eventReplacement = {
    "mousedown": ["touchstart mousedown", "mousedown"],
    "mouseup": ["touchend mouseup", "mouseup"],
    "click": ["touchstart click", "click"],
    "mousemove": ["touchmove mousemove", "mousemove"]
};


for (i in eventReplacement) {
    if (typeof window["on" + eventReplacement[i][0]] == "object") {
        eventMap[i] = eventReplacement[i][0];
    } 
    else {
        eventMap[i] = eventReplacement[i][1];
    };
 };
})();
然后在事件中使用如下内容:

$(".yourDiv").on(eventMap.mouseup, function(event) {
      //your code
}

要使@Mickey的代码在Edge和internet explorer上工作,请在要进行模拟的元素的.css中添加以下行:

.areaWhereSimulationHappens {
    overflow: hidden;
    touch-action: none;
    -ms-touch-action: none;
}

这些行阻止了edge和IE的默认触摸。但是如果您将其添加到错误的元素,那么它也会禁用滚动(在edge和IE的触摸设备上默认情况下会发生这种情况)。

您可以发布到目前为止的代码吗?我正在使用YUI库中的这两个滑块:并非所有的touchmove事件都会转换为mousemove事件!e、 g.如果您只想在屏幕上向下滚动,touchmove事件也会被触发,但会转化为滚动事件。。。!我发现这一点非常有用,同时我还试图弄明白为什么一个信息亭的触摸屏显示器不会像鼠标那样在屏幕上画图。Chrome和Firefox在这方面似乎有不同的行为。在Firefox中,我看到您仍然可以从触摸屏上获得鼠标移动事件,但在chrome中并非如此。initMouseEvent方法现在已被弃用()也许“touchcancel”事件也应该被映射?可能将“mouseup”改为“touchend”@YuriGor可能
mouseout
是一个更好的选择,尤其是当用户拖离屏幕时。如果您对其进行向下投票,请添加原因!