Hammer.js事件互斥重叠之requireFailure与recognizeWith

本文简单记录下hammer.js的事件重叠与互斥问题,如有不当之处望指出,谢谢!

版本:Hammer.JS - v2.0.8

先说说recognizeWith,这个很好理解,就是可以同时辨识多重手势事件,比如,单击.recognizeWith(双击) 的情况下,点击两下,这会触发单击(调用两次单击回调)和双击事件(调用一次双击回调);

requireFailure的作用大家可以自己看原文,我的个人理解比如,在 单击.recognizeWith(双击) 然后 单击.requireFailure(双击) 的情况下,它会在第二次点击后,也就是双击后触发 单击的回调函数,而不会触发双击的回调,简单点理解就是满足单击的条件后,双击就不用管它了;

以下是实验代码,有需要的可以跑一下看看:

var doc = document;
(function(){
    var box = doc.getElementById("box");
    var hm = new Hammer.Manager(box);
    var singletap = new Hammer.Tap({event: 'singletap'});
    var doubletap = new Hammer.Tap({event: 'doubletap', taps: 2});

    var panh = new Hammer.Pan({event: 'panh', direction: Hammer.DIRECTION_HORIZONTAL});
    var panv = new Hammer.Pan({event: 'panv', direction: Hammer.DIRECTION_VERTICAL});
    var swipe = new Hammer.Swipe();

    hm.add([panh, panv, singletap, doubletap, swipe]);

    doubletap.recognizeWith(singletap);
    singletap.requireFailure(doubletap);
    //swipe.recognizeWith(panh);
    //panv.requireFailure(panh);
    //swipe.requireFailure(panh);
    //*
    hm.on('panh', function(e) {
        console.log('panh event');
        console.log(e);
    });
    hm.on('panv', function(e) {
        console.log('panv event');
        console.log(e);
    });
    //*/
    hm.on('doubletap', function(e) {
        console.log('doubletap event');
        console.log(e);
    });
    hm.on('singletap', function(e) {
        console.log('singletap event');
        console.log(e);
    });
    hm.on('swipe', function(e) {
        console.log('swipe event');
        console.log(e);
    });
})();

你可能感兴趣的:(web前端,Javascript)