Click Delay On IPhone and Suppressing Input Focus

自作多情 提交于 2019-12-01 11:34:10

You have to capture your event on touchstart if you want to get the fastest possible responsiveness. Otherwise you'll be doomed with this input lag.

You have to remember though that capturing event on touchstart and responding to it makes it impossible to cancel action by dragging your finger out of responsive area.

I have personally used this in my PhoneGap html/js based iphone application and it worked perfect. The only solution to give this almost-native feel.

Now regarding your problem - have you tried to stop the propagation of the event? It should solve your problem.

$('.button').bind('touchstart', function(e){
    e.stopPropagation();
    // do something...
});

hope it helps,

Tom

My colleagues and I developed an open source library called FastClick for getting rid of the click delay in Mobile Safari. It converts touches to clicks and handles those special cases for input and select elements cleanly.

It's as easy as instantiating it on the body like so: new FastClick(document.body), then listening for click events as usual.

Armel Larcier

I made Matt's FastClick a jquery plugin:

stackoverflow link

Just had a comment about the onClick handler being called without the necessary scope being passed. I updated the code to make it work. There also seems to be a problem when input elements lie under the ghost event's position: the focus event gets triggered without being busted.

I see two problems in the question. One is handling the delay in click and the other is handling input focus. Yes, both of these have to be handled properly in mobile web.

  1. The delay in click has deeper reasons. The reason for this 300ms delay is explained very well in this article. Responsiveness of the click event.

    Thankfully this problem is well known and solved by many libraries. JQTouch, JQuery Mobile, JQuery.tappable, Mootools-mobile, and tappable

    Most of these libraries create a new event called tap. you can use the tap event similar to the click event. This jquery-mobile event handling might help.

     $("#tappableElement").tap(function(){
       // provide your implementation here. this is executed immediately without the 300ms delay.
     });
    
  2. Now, the second problem is with the handling of input focus. There is a noticeable delay here also. This can be solved by forcing focus on the element immediately for one of the touchstart or touchend events. This JQuery event handling might help.

    $('#focusElement').bind('touchstart', function(e){
        $(this).focus();
    });
    
    $('#focusElement').focus(function(e){
        // do your work here.
    });
    

    You can do e.stopPropagation in 'touchstart' event handling to avoid propagation. But I would strongly advise against return false; or e.preventDefault as that would stop default functionality like copy/paste, selecting text etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!