4

Here is my code... just a simple test as I have never used hammer.js before:

var hammerTime = new Hammer($('#hammerTarget').get(0));

hammerTime.add(new Hammer.Pinch({event: 'pinch'})); // Add pinch gesture events.

hammerTime.on('pinchin', function(event) {
    console.log('hammer pinchin');

}).on('pinchout', function(event) {
    console.log('hammer pinchout');

}).on('pinchend', function(event) {
    console.log('hammer pinchend');
});

This works fine, I can detect the pinch, but now on my pinch target I can't zoom the browser any more? How can I use the pinch event and allow the default browser pinch zooming? I need to do some stuff on pinch but I still want people to be able to zoom in.

I'm using hammer.js 2.0.4 in case it matters.

1 Answer 1

6

I was having a hard time with this too, until I came across the touch-action property. by setting it to auto, hammer stops blocking the events, and allows the browser to do it's own zoom.

var hammerTime = new Hammer($('#hammerTarget').get(0), {touchAction : 'auto'});

hammerTime.add(new Hammer.Pinch({event: 'pinch'})); // Add pinch gesture events.

hammerTime.on('pinchin', function(event) {
    console.log('hammer pinchin');

}).on('pinchout', function(event) {
    console.log('hammer pinchout');

}).on('pinchend', function(event) {
    console.log('hammer pinchend');
});

See the doc : http://hammerjs.github.io/touch-action/

1
  • 2
    The docs at the linked page say Hammer would probably break. You have to call preventDefault manually to fix this. You should only use this if you know what you’re doing. And course I don't know what I'm doing, because I haven't been able to find anything that coherently and accurately describes exactly when and where I need to call preventDefault!
    – Michael
    Commented Feb 18, 2018 at 18:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.