简体   繁体   中英

Manually trigger event in HammerJS 2.0

I am trying to manually fire the event in HammerJS but as of now I cannot see any progress. Moreover there are links here which takes you HammerJS documentation wiki which is for the version 1.

Can someone tell me how to fire an event manually using HammerJS 2.0 ?

This is how HammerJS tests trigger a touch event.

function createTouchEvent (name, x, y, identifier) {
    var event = document.createEvent('Event');
    event.initEvent('touch' + name, true, true);

    event.touches = event.targetTouches = [{
        clientX: x,
        clientY: y,
        identifier: identifier || 0
    }];

    //https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent.changedTouches
    event.changedTouches = [{
        clientX: x,
        clientY: y,
        identifier: identifier || 0
    }];

    return event;
}

function dispatchTouchEvent (el, name, x, y, identifier) {
    var event = createTouchEvent(name, x, y, identifier);
    el.dispatchEvent(event);
}

you can for example fire a tap:

dispatchTouchEvent(element, 'start', 50, 50);
dispatchTouchEvent(element, 'end', 50, 50);

or a pan:

dispatchTouchEvent(element, 'start', 50, 50);
dispatchTouchEvent(element, 'move', 70, 50);
dispatchTouchEvent(element, 'end', 70, 50);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM