【问题标题】:Is there a way that a touch start event will not trigger the click event?有没有办法让触摸开始事件不会触发点击事件?
【发布时间】:2012-04-18 22:06:06
【问题描述】:

当访问者点击图片时,click 事件将被触发。但是,当有人触摸图像时,将触发相同的 click 事件,即使 touchstart 事件也可用。

我喜欢实际点击(鼠标)事件和触摸事件的不同行为。奇怪的是,在智能手机上使用时甚至会触发mouseup 事件。无论如何,您可以将鼠标与触摸事件分开吗?

【问题讨论】:

    标签: javascript events touch mouse


    【解决方案1】:
    event.preventDefault();
    

    成功了,希望对大家有所帮助!

    【讨论】:

    • 如果您只是将event.preventDefault() 添加到touchstart 回调中,这将不起作用。至少不是 iOS 8.4 Safari 中的链接。您必须将其添加到 click 事件处理程序。
    【解决方案2】:

    你可以标准化一个事件..

    查看我对这个问题的回答:

    Click event called twice on touchend in iPad

    您也可以在 jQuery mobile 源代码中寻找灵感:http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.js 从第 982 行开始

    /* 
    * "events" plugin - Handles events
    */
    
    (function( $, window, undefined ) {
    
    // add new event shortcuts
    $.each( ( "touchstart touchmove touchend orientationchange throttledresize " +
                        "tap taphold swipe swipeleft swiperight scrollstart scrollstop" ).split( " " ), function( i, name ) {
    
        $.fn[ name ] = function( fn ) {
            return fn ? this.bind( name, fn ) : this.trigger( name );
        };
    
        $.attrFn[ name ] = true;
    });
    ....
    

    查看点击事件:(第 1049 行)

    $.event.special.tap = {
    

    【讨论】:

    • 感谢您的详尽回答。唯一的问题是,在这种情况下,您现在针对的是“特殊设备”,即 iPhone。我宁愿希望所有支持 touchstart 的设备都将 click 和 touchstart 区别对待。我希望有一个简单的 var 可以设置为禁用。
    【解决方案3】:

    在 touchstart 后防止点击是一种复杂的方法。 touchstart 后传播的点击称为幻点击。

    Google 已经实施了一个解决方案。给你。。

    http://code.google.com/mobile/articles/fast_buttons.html#ghost

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 1970-01-01
      • 2019-11-02
      • 2012-06-30
      • 1970-01-01
      • 2011-07-29
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      相关资源
      最近更新 更多