Double tap on mobile safari

后端 未结 3 1018
南笙
南笙 2021-01-02 15:56

Is there a way to bind to a double-tap event (in a single line of code) for mobile safari? Or, the alternative is to implement it intercepting two single-tap events that hap

相关标签:
3条回答
  • 2021-01-02 16:19

    If you want to have working double click both on browser and IOS platform, you should have the following code:

    jQuery.event.special.dblclick = {
        setup: function(data, namespaces) {
            var agent = navigator.userAgent.toLowerCase();
            if (agent.indexOf('iphone') >= 0 || agent.indexOf('ipad') >= 0 || agent.indexOf('ipod') >= 0) {
                var elem = this,
                    $elem = jQuery(elem);
                $elem.bind('touchend.dblclick', jQuery.event.special.dblclick.handler);
            } else {
                var elem = this,
                    $elem = jQuery(elem);
                $elem.bind('click.dblclick', jQuery.event.special.dblclick.handler);
            }
        },
        teardown: function(namespaces) {
            var agent = navigator.userAgent.toLowerCase();
            if (agent.indexOf('iphone') >= 0 || agent.indexOf('ipad') >= 0 || agent.indexOf('ipod') >= 0) {
                var elem = this,
                    $elem = jQuery(elem);
                $elem.unbind('touchend.dblclick');
            } else {
                var elem = this,
                    $elem = jQuery(elem);
                $elem.unbind('click.dblclick', jQuery.event.special.dblclick.handler);
            }
        },
        handler: function(event) {
            var elem = event.target,
                $elem = jQuery(elem),
                lastTouch = $elem.data('lastTouch') || 0,
                now = new Date().getTime();
            var delta = now - lastTouch;
            if (delta > 20 && delta < 500) {
                $elem.data('lastTouch', 0);
                $elem.trigger('dblclick');
            } else {
                $elem.data('lastTouch', now);
            }
        }
    };
    

    Try it here:

    http://jsfiddle.net/UXRF8/

    0 讨论(0)
  • 2021-01-02 16:19

    Override dblclick event and use bind, live, on, delegate like for any other event:

    jQuery.event.special.dblclick = {
        setup: function(data, namespaces) {
            var elem = this,
                $elem = jQuery(elem);
            $elem.bind('touchend.dblclick', jQuery.event.special.dblclick.handler);
        },
    
        teardown: function(namespaces) {
            var elem = this,
                $elem = jQuery(elem);
            $elem.unbind('touchend.dblclick');
        },
    
        handler: function(event) {
            var elem = event.target,
                $elem = jQuery(elem),
                lastTouch = $elem.data('lastTouch') || 0,
                now = new Date().getTime();
    
            var delta = now - lastTouch;
            if(delta > 20 && delta<500){
                $elem.data('lastTouch', 0);
                $elem.trigger('dblclick');
            }else
                $elem.data('lastTouch', now);
        }
    };
    
    0 讨论(0)
  • 2021-01-02 16:28

    Short answer: you must implement via two clicks.

    Actual answer: Here is a jQuery-free implementation of double-tap for mobile safari which only requires one line of code (to enable dblclick event):

    • https://gist.github.com/2927073

    In addition, you will likely need to disable mobile Safari's default zoom with this meta tag:

    <meta name="viewport" content="width=device-width,user-scalable=no" />
    
    0 讨论(0)
提交回复
热议问题