开发者

certain touch events cause multiple events, how can I limit to the desired event?

So if I tap or taphold it works as expected, but if I swipe I should get two events. The swipe and the swipedirection, but I also get a tap event as well. is there a way to remove the tap event if swipe or swipedirection occur开发者_JS百科s?

Live Example:

  • http://jsfiddle.net/phillpafford/hJtAQ/11/

HTML:

<div data-role="page" id="home"> 
    <div data-role="content">
        <div id="display-event" class="add-border">
            <p>Tap, TapHold (for 1 second), Swipe, SwipeRight or SwipeLeft</p>
        </div>
    </div>
</div>

JS:

function bindEvent(element) {
    element.bind('tap taphold swipe swiperight swipeleft', function(event, ui) {
       alert('Event: '+event.type); 
    });
}

bindEvent($('#display-event'));

Related Question: Swipe event triggers tap event for ipod touch

Update: - http://jsfiddle.net/hJtAQ/31/ (using Jaspers method)


If you want to only validate the first event that fires you can set a timeout and preventDefault() for the rest of the events fired:

function bindEvent(element) {
    element.bind('tap taphold swipe swiperight swipeleft', function(event, ui) {
       if (event_ok == false) {
           event.preventDefault();
           return false;
       } else {
           event_ok = false;
           setTimeout(function () {
               event_ok = true;
           }, 500);
           alert('Event: '+event.type);
       }
    });
}

var event_ok = true;
bindEvent($('#display-event'));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜