如果元素“ #menu”被触摸,我想获取touchstart和touchend的位置。

我编写了这段代码,但是该事件似乎根本没有触发:

        var lastLoc = 0;

        $(document).on('touchstart', '#menu', function(e) {
            lastLoc = e.originalEvent.touches[0].pageX;
        });

        $(document).on('touchend', '#menu', function(e) {
            var newLoc = e.originalEvent.touches[0].pageX;
            alert("lastLoc = "+lastLoc+", newLoc = "+newLoc);
        });


据我所知这应该工作。

提前致谢。

最佳答案

从逻辑上讲,touchend事件应具有零个touch点,只有changedTouches个。

Here's a demo.

function startHandler (e) {
    window.console.log('Start:', e.originalEvent.touches[0].pageX);
}

function endHandler (e) {
    window.console.log('End:', e.originalEvent.changedTouches[0].pageX);
}



$(document).on('touchstart', '#menu', startHandler);
$(document).on('touchend', '#menu', endHandler);


另请参见:Simulating touchstart and touchend events?如果无法测试触摸事件。

10-08 07:12
查看更多