iOS中的jQuery Mobile tap和taphold敏感度问题

时间:2012-08-07 10:33:13

标签: jquery-mobile

在我的jQuery Mobile应用程序中,我想使用tap和taphold事件。我尝试使用将事件处理程序绑定到这些事件的标准方法,但是在taphold事件的情况下,tap事件总是触发,所以我使用了我在stackoverflow上找到的以下方法: jQuery calling click event after taphold event

$("#list li").live('vmousedown vmouseup', function (event) 
{
    if (event.type == 'vmousedown')
{
        tapTime = new Date().getTime();
    } 
    else 
    {
        //event.type == 'vmouseup'
        //here you can check how long the `tap` was to determine what do do
        duration = (new Date().getTime() - tapTime);

        //The tap code
        if(duration >250 && duration <750)
        {

        }
        //The taphold code

        else if (duration >=750) {

 } 

现在,在iOS 5的iPhone上,我遇到的问题是,当我向下滚动列表时,点击事件被触发并且选择了一个项目。我试图增加点击事件的持续时间,但似乎在iOS中没有效果。有什么建议?

1 个答案:

答案 0 :(得分:1)

[尝试和测试] 我检查了jQuery Mobile的实现。他们每次在'vmouseup'''taphold'之后开始'tap'活动。

如果'taphold'被解雇,解决方法是不会触发'tap'事件。根据需要创建自定义事件或修改源,如下所示:

$.event.special.tap = {
    tapholdThreshold: 750,

    setup: function() {
        var thisObject = this,
            $this = $( thisObject );

        $this.bind( "vmousedown", function( event ) {

            if ( event.which && event.which !== 1 ) {
                return false;
            }

            var origTarget = event.target,
                origEvent = event.originalEvent,
                /****************Modified Here**************************/
                tapfired = false,
                timer;

            function clearTapTimer() {
                clearTimeout( timer );
            }

            function clearTapHandlers() {
                clearTapTimer();

                $this.unbind( "vclick", clickHandler )
                    .unbind( "vmouseup", clearTapTimer );
                $( document ).unbind( "vmousecancel", clearTapHandlers );
            }

            function clickHandler( event ) {
                clearTapHandlers();

                // ONLY trigger a 'tap' event if the start target is
                // the same as the stop target.
                /****************Modified Here**************************/
                //if ( origTarget === event.target) {
                 if ( origTarget === event.target && !tapfired) {
                     triggerCustomEvent( thisObject, "tap", event );
                 }
            }

            $this.bind( "vmouseup", clearTapTimer )
                .bind( "vclick", clickHandler );
            $( document ).bind( "vmousecancel", clearTapHandlers );

            timer = setTimeout( function() {
                tapfired = true;/****************Modified Here**************************/
                triggerCustomEvent( thisObject, "taphold", $.Event( "taphold", { target: origTarget } ) );
            }, $.event.special.tap.tapholdThreshold );
        });
    }
};