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

前端 未结 30 3060
抹茶落季
抹茶落季 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: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
         }
    }
    

提交回复
热议问题