これらのコードを使用して Web サイトにタッチ機能を実装しましたが、iPad/iPhone で正常に動作します。
// registering touch events
function initTouchEvents() {
// if a device is a touch device
if (window.Touch) {
document.addEventListener("touchstart", touchHandler, true);
document.addEventListener("touchmove", touchHandler, true);
document.addEventListener("touchend", touchHandler, true);
document.addEventListener("touchcancel", touchHandler, true);
}
}
// making items sortable in touch screen devices
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;
}
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);
if (event.type == "touchmove") {
event.preventDefault();
}
}
window.Touch
しかし、タッチ スクリーン PC で自分の Web サイトをテストしたところ、動作せず、iPhone/iPad でしか動作しないことがわかりました。typeof(window.ontouchstart != 'undefined')
また、andのような他のさまざまなイベントも試し('ontouchstart' in window || typeof TouchEvent != "undefined")
ましたが、タッチスクリーンであることを検出せず、タッチ移動のイベントを登録しません。
ここで私が求めているのは、すべてのタッチ デバイス (IOS/Android/Windows/OSX) を検出し、イベントを登録できる JavaScript のイベントです。