【发布时间】:2012-04-18 09:19:35
【问题描述】:
我正在尝试让 jQuery 拖放与 iPad 触摸事件很好地配合使用。我在网上找到了这段代码:
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();
}
如果我将 touchHandler 附加到 document touchstart/move/end 似乎可以正常工作,但是不允许在 iPad 上进行本机缩放/滚动,所以我试图仅将此处理程序附加到可拖动对象本身.
我看到的问题是 event.changeTouches 由于某种原因总是未定义,正如您可以在 http://jsfiddle.net/myLj2/1/ 中看到的那样。我想不出为什么触摸事件总是具有未定义的changeTouches 属性的原因,因此,此代码将不起作用。有什么想法吗?
【问题讨论】:
标签: javascript jquery jquery-ui touch-event