2012-02-21 112 views
3

我使用這個非常棒的代碼來模擬Android http://pervasivecode.blogspot.co.nz/2011/11/android-phonegap-active-css-pseudo.html上的觸摸事件。工程很好,但太敏感。例如,如果你有一個列表,並且當你觸摸列表上的任何東西,它就會突出顯示一個鏈接。touchstart太敏感了

$(document).ready(function(){ 
if (navigator.userAgent.toLowerCase().indexOf("android") > -1) { 
$('a') 
.bind("touchstart", function() { 
    $(this).addClass("fake-active"); 

     }) 
.bind("touchend", function() { 
    $(this).removeClass("fake-active"); 
    }); 
} 
    }); 

是否有可能延遲班級更改或是否存在可以使用的另一個觸發器?

回答

1

爲了避免在滾動時激活鏈接,可以使用touchend觸發類更改,並檢查滾動(如果存在)。像這樣的東西...

if (navigator.userAgent.toLowerCase().indexOf("android") > -1){ 

    var scroll = 0; 
    $('a').on("touchstart",function(){ 
     $('a').removeClass("fake-active"); 
    }).on("touchend",function(){ 
     if(scroll == 0){ 
      $(this).addClass("fake-active"); 
     } 
     scroll = 0; //Ideally should be set when scrolling is finished 
    }); 

    $('ELEMENT-THAT-IS-SCROLLING').scroll(function(){ 
    scroll = 1; 
    }); 

}