【问题标题】:How can I keep the browser pinch to zoom and use the hammer.js pinch events?如何保持浏览器捏缩放并使用hammer.js捏事件?
【发布时间】:2015-05-26 07:40:37
【问题描述】:

这是我的代码...只是一个简单的测试,因为我以前从未使用过hammer.js:

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');
});

这很好用,我可以检测到捏合,但是现在在我的捏合目标上我不能再缩放浏览器了?如何使用捏合事件并允许默认浏览器捏合缩放?我需要在捏合时做一些事情,但我仍然希望人们能够放大。

我使用的是hammer.js 2.0.4,以防万一。

【问题讨论】:

    标签: javascript hammer.js pinchzoom


    【解决方案1】:

    我也遇到了困难,直到我遇到了 touch-action 属性。通过将其设置为自动,锤子停止阻止事件,并允许浏览器进行自己的缩放。

    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');
    });
    

    查看文档:http://hammerjs.github.io/touch-action/

    【讨论】:

    • 链接页面上的文档说 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. 当然我知道我在做什么,因为我找不到任何连贯的东西并准确地描述了我需要调用 preventDefault 的时间和地点!
    猜你喜欢
    • 2013-08-03
    • 2017-04-23
    • 2018-09-01
    • 2012-04-06
    • 2015-09-28
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多