How can I keep the browser pinch to zoom and use the hammer.js pinch events?

帅比萌擦擦* 提交于 2019-12-10 18:55:50

问题


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:


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/



来源:https://stackoverflow.com/questions/30452422/how-can-i-keep-the-browser-pinch-to-zoom-and-use-the-hammer-js-pinch-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!