How to bind 'touchstart' and 'click' events but not respond to both?

前端 未结 30 3002
抹茶落季
抹茶落季 2020-11-22 14:08

I\'m working on a mobile web site that has to work on a variety of devices. The one\'s giving me a headache at the moment are BlackBerry.

We need to support both key

相关标签:
30条回答
  • 2020-11-22 14:30

    Update: Check out the jQuery Pointer Events Polyfill project which allows you to bind to "pointer" events instead of choosing between mouse & touch.


    Bind to both, but make a flag so the function only fires once per 100ms or so.

    var flag = false;
    $thing.bind('touchstart click', function(){
      if (!flag) {
        flag = true;
        setTimeout(function(){ flag = false; }, 100);
        // do something
      }
    
      return false
    });
    
    0 讨论(0)
  • 2020-11-22 14:30

    check fast buttons and chost clicks from google https://developers.google.com/mobile/articles/fast_buttons

    0 讨论(0)
  • 2020-11-22 14:30

    Being for me the best answer the one given by Mottie, I'm just trying to do his code more reusable, so this is my contribution:

    bindBtn ("#loginbutton",loginAction);
    
    function bindBtn(element,action){
    
    var flag = false;
    $(element).bind('touchstart click', function(e) {
        e.preventDefault();
        if (!flag) {
            flag = true;
            setTimeout(function() {
                flag = false;
            }, 100);
            // do something
            action();
        }
        return false;
    });
    
    0 讨论(0)
  • 2020-11-22 14:30

    Why not use the jQuery Event API?

    http://learn.jquery.com/events/event-extensions/

    I've used this simple event with success. It's clean, namespaceable and flexible enough to improve upon.

    var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent);
    var eventType = isMobile ? "touchstart" : "click";
    
    jQuery.event.special.touchclick = {
      bindType: eventType,
      delegateType: eventType
    };
    
    0 讨论(0)
  • 2020-11-22 14:31

    Well... All of these are super complicated.

    If you have modernizr, it's a no-brainer.

    ev = Modernizr.touch ? 'touchstart' : 'click';
    
    $('#menu').on(ev, '[href="#open-menu"]', function(){
      //winning
    });
    
    0 讨论(0)
  • 2020-11-22 14:32

    Generally you don't want to mix the default touch and non-touch (click) api. Once you move into the world of touch it easier to deal only with the touch related functions. Below is some pseudo code that would do what you want it to.

    If you connect in the touchmove event and track the locations you can add more items in the doTouchLogic function to detect gestures and whatnot.

    var touchStartTime;
    var touchStartLocation;
    var touchEndTime;
    var touchEndLocation;
    
    $thing.bind('touchstart'), function() {
         var d = new Date();
         touchStartTime = d.getTime();
         touchStartLocation = mouse.location(x,y);
    });
    
    $thing.bind('touchend'), function() {
         var d = new Date();
         touchEndTime= d.getTime();
         touchEndLocation= mouse.location(x,y);
         doTouchLogic();
    });
    
    function doTouchLogic() {
         var distance = touchEndLocation - touchStartLocation;
         var duration = touchEndTime - touchStartTime;
    
         if (duration <= 100ms && distance <= 10px) {
              // Person tapped their finger (do click/tap stuff here)
         }
         if (duration > 100ms && distance <= 10px) {
              // Person pressed their finger (not a quick tap)
         }
         if (duration <= 100ms && distance > 10px) {
              // Person flicked their finger
         }
         if (duration > 100ms && distance > 10px) {
              // Person dragged their finger
         }
    }
    
    0 讨论(0)
提交回复
热议问题