栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

我确定这是您想要的:

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/596868.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号