仅使用CSS的解决方案
将touch-action: manipulation
添加到您想要禁用双击缩放的任何元素上,例如使用以下disable-dbl-tap-zoom
类:
.disable-dbl-tap-zoom {
touch-action: manipulation;
}
<button>plain button</button>
<button class="disable-dbl-tap-zoom">button with disabled double-tap zoom</button>
<p>A <b>plain</b> paragraph. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</p>
<p class="disable-dbl-tap-zoom">A paragraph <b>with disabled double-tap zoom</b>. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.</p>
touch-action
文档中(重点是我的):input[type="button"]{ touch-action: manipulation; }
后,在iOS 14.6上(使用Safari,手机而非平板电脑)运行良好。当我在页面其他地方双击时,它会放大或有时选择内容,但当我点击按钮或者仅靠近按钮时,它总是会触发按钮而不会做其他任何事情。对于以后的读者,也许你可以更新顶部的编辑内容以表明它适用于按钮,但对于其他元素,可能(因为Adam Silver称它不适用于图像)不起作用。 - Luc<head>
<title>Site</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
etc...
</head>
我最近使用过它,在iPad上运行良好。 我没有在Android或其他设备上进行测试(因为该网站仅在iPad上显示)。
禁用双击缩放的CSS全局样式(适用于任何元素):
* {
touch-action: manipulation;
}
操作
启用平移和捏合缩放手势,但禁用其他非标准手势,如双击缩放。
谢谢Ross,我的答案是他的延伸:https://dev59.com/eWkv5IYBdhLWcg3waAJT#53236027
document.getElementById('root').addEventListener('click', () => {})
- Sergei我知道这可能有点老了,但我找到了一个完美解决我的问题的方法。无需疯狂的元标签和停止内容缩放。
我不确定它在跨设备上是否有效,但它完全按照我想要的方式工作。
$('.no-zoom').bind('touchend', function(e) {
e.preventDefault();
// Add your code here.
$(this).click();
// This line still calls the standard click event, in case the user needs to interact with the element that is being clicked on, but still avoids zooming in cases of double clicking.
})
这将简单地禁用普通的触摸功能,然后再次调用标准的点击事件。这可以防止移动设备缩放,但其他方面功能正常。
编辑:现在已经经过时间测试,并在几个实时应用程序中运行。看起来它可以在各种浏览器和平台上100%运行。上述代码应该可以作为大多数情况下的复制粘贴解决方案,除非您想在点击事件之前自定义行为。
pointer-events: none
即可。 - Pixelomo我只是想正确回答我的问题,因为有些人不会阅读回答下面的评论。所以这里是:
(function($) {
$.fn.nodoubletapzoom = function() {
$(this).bind('touchstart', function preventZoom(e) {
var t2 = e.timeStamp
, t1 = $(this).data('lastTouch') || t2
, dt = t2 - t1
, fingers = e.originalEvent.touches.length;
$(this).data('lastTouch', t2);
if (!dt || dt > 500 || fingers > 1) return; // not double-tap
e.preventDefault(); // double tap - prevent the zoom
// also synthesize click events we just swallowed up
$(this).trigger('click').trigger('click');
});
};
})(jQuery);
这不是我写的,我只做了一些修改。我在这里找到了仅适用于iOS版本: https://gist.github.com/2047491 (感谢Kablam)
如果你需要一份不需要jQuery的版本,我修改了Wouter Konecny的答案(该答案也是通过修改Johan Sundström的这个代码片段而创建的),使其使用原生JavaScript。
function preventZoom(e) {
var t2 = e.timeStamp;
var t1 = e.currentTarget.dataset.lastTouch || t2;
var dt = t2 - t1;
var fingers = e.touches.length;
e.currentTarget.dataset.lastTouch = t2;
if (!dt || dt > 500 || fingers > 1) return; // not double-tap
e.preventDefault();
e.target.click();
}
然后在touchstart
上添加事件处理程序,调用此函数:
myButton.addEventListener('touchstart', preventZoom);
myButton.addEventListener('touchstart', preventZoom);
- martin jakubik按照这个答案https://dev59.com/m1gQ5IYBdhLWcg3w3Xsy#42288386所述,您应该将CSS属性touch-action
设置为none
。
.disable-doubletap-to-zoom {
touch-action: none;
}
* {
-ms-touch-action: manipulation;
touch-action: manipulation;
}
禁用移动设备上的双击缩放 (2023年IOS Safari解决方案):
我发现对于移动Safari浏览器,使用meta标签方法并不是可行的解决方案。以下是适用于我的解决方案。
有效的解决方案:
.selector {
touch-action: manipulation;
}
通过简单地添加一个 manipulation 触摸动作,所有应用以下规则的按钮将不会在连续点击按钮时缩放。
示例网站:计算器应用
很不幸,大部分以上的代码都无法正常工作
这几行简单的代码就可以解决问题
document.addEventListener('dblclick', function(event) {
event.preventDefault();
}, { passive: false });
-webkit-text-size-adjust: none; -webkit-user-select: none; - Udayraj Deshmukh