【问题标题】:Javascript - touchstart and touchend locationsJavascript - touchstart 和 touchend 位置
【发布时间】:2015-02-10 13:01:52
【问题描述】:

如果元素“#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);
        });

据我所知,这应该可行。

提前致谢。

【问题讨论】:

    标签: javascript html touch


    【解决方案1】:

    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?

    【讨论】:

      【解决方案2】:

      您需要在智能手机或平板电脑等可触摸表面上测试触摸事件。 在谷歌浏览器中有一种方法可以模拟这个功能。

      【讨论】:

        【解决方案3】:

        抱歉,我最终使用了 jquery mobile 的“swipeleft” 无论如何谢谢:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-05-11
          • 1970-01-01
          • 1970-01-01
          • 2014-05-16
          • 2012-06-05
          • 1970-01-01
          • 2017-04-25
          • 1970-01-01
          相关资源
          最近更新 更多