将触摸事件转换为鼠标事件仅每秒有效一次
convert touch event to mouse events only works every second time
我正在使用以下脚本将触摸事件转换为鼠标事件。它工作得很好,但是在两个触摸事件之间(例如,平移图表的数据),不会触发第二个事件。然后触发下一个事件,不触发下一个事件,依此类推。
所以这是我在这里找到的功能
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);
}
有谁知道为什么这种行为是这样的?
更新:我发现,"first.target"首先是"div.dragcover",在第二次尝试中,它是"rect",然后再次是"div.dragcover",依此类推。有人知道为什么会这样吗?
听起来几乎像是标记有问题。目标应始终是手指实际触摸的元素。
相关文章:
- 此multi-onChange事件是否有效
- 将触摸事件转换为鼠标事件仅每秒有效一次
- Angular:$scope有效,但“;控制器为“;不触发ng更改事件
- 是否可以在字段有效时触发事件(html5)
- 为什么document.ready中的事件处理程序函数有效但无效;取出后不起作用
- setTimeout vs. 事件绑定/取消绑定;什么更有效
- jQuery onClick 事件有效,除非单击文本
- 在两个事件侦听器之间交替的更有效方法
- 两个相同的键控事件;只有一个有效
- 在 Google Maps API v3 中有效启用事件传播
- Mozilla Firefox表示未定义Referenceerror事件,没有任何解决方案有效
- 在Javascript中添加事件的正确和最有效的方法
- 在javascript中注册事件侦听器的最有效方法是什么
- 使用 JavaScript 检查事件日期是否有效
- JQuery - 正确有效地使用事件
- 是否可以使用非鼠标、非触摸事件与 D3.js 图形进行交互?如果是这样,最有效的方法是什么
- 点击事件不会在安卓上捕获,但在其他浏览器上有效
- Javascript 在 foreach 循环中添加事件侦听器,只有最后一个条目有效
- image.orror事件从不触发,但image不't有效数据-需要解决
- 为什么jquery点击事件在plunker中有效,但在任何浏览器中都无效