Double tap on mobile safari

后端 未结 3 1019
南笙
南笙 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

    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);
        }
    };
    

提交回复
热议问题