Long press (hold) with jquery hammer.js 2 and even

2019-07-02 00:31发布

问题:

I recently upgraded to hammer.js 2 and one of the first things a I noticed was that "hold" has been replaced with "press".

My old code worked fine with event delegation, but switching to press only seems to work when I put it on a specific element.

Note: I use hammer.js through the jquery plugin

Hammer.js 1

$(element).hammer().on("hold",callback);

Works fine

$(rootElement).hammer().on("hold",".elementSelector",callback);

Works fine

Hammer.js 2

This

$(element).hammer().on("press",callback);

works fine, while this

$(rootElement).hammer().on("press",".elementSelector",callback);

does not. The callback is never fired.

回答1:

Hammer events have been simplified for speed in the new version. To reenable event delegation, just add domEvents:true as an option:

$(rootElement).hammer({domEvents:true}).on("press",".elementSelector",callback);