android js touch事件传递,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/*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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值