3

これらのコードを使用して 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 のイベントです。

4

2 に答える 2

4

Phantom Limbは、デスクトップブラウザにタッチイベントをシミュレートさせます

https://github.com/brian-c/phantom-limb

于 2012-04-17T09:45:06.387 に答える
1

クロムのコンソール設定に「タッチイベントのエミュレート」があると思います。それを確認してみてください。

CTRL+ SHIFT+を押すIか、単に doF12を押してから、右下の歯車アイコンをクリックします。次に、「タッチイベントをエミュレートする」にチェックを入れます。タッチイベントをトリガーする必要があると思います。

于 2012-04-17T09:39:40.207 に答える