双击手机游猎

时间:2012-04-23 09:43:18

标签: javascript iphone ipad mobile-safari

有没有办法绑定到移动游猎的双击事件(在一行代码中)?或者,另一种方法是实现它拦截在一些短暂的给定时间内发生的两个单击事件(例如:http://appcropolis.com/blog/implementing-doubletap-on-iphones-and-ipads/)?

3 个答案:

答案 0 :(得分:7)

简答:您必须通过两次点击来实现。

实际答案:这是一个免费的jQuery实现双击移动版Safari,只需要一行代码(启用dblclick事件):

此外,您可能需要使用此元标记disable mobile Safari's default zoom

<meta name="viewport" content="width=device-width,user-scalable=no" />

答案 1 :(得分:3)

如果您想在浏览器和IOS平台上双击工作,您应该拥有以下代码:

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

在这里试试:

http://jsfiddle.net/UXRF8/

答案 2 :(得分:0)

覆盖dblclick个活动,并使用bindliveondelegate等任何其他活动:

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