如何在 Mobile Safari 上禁用视口缩放?
我已经尝试了所有这三个都无济于事:
<meta name=”viewport” content=”width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;” />
<meta name=”viewport” content=”width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=false;” />
<meta name=”viewport” content=”width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=no;” />
每个都是我发现谷歌搜索或SO搜索推荐的不同值,但“user-scalable=X”值似乎都不起作用
我还尝试用逗号而不是分号来分隔值,但没有成功。然后我尝试只存在user-scalable
值,仍然没有运气。
更新
从苹果网站得到这个并且它有效:
<meta name="viewport" content="width=device-width, user-scalable=no" />
事实证明问题是非标准引号,因为我从使用它们的网站复制了元标记,哎呀
I've tried all three of these to no avail:
<meta name=”viewport” content=”width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;” />
<meta name=”viewport” content=”width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=false;” />
<meta name=”viewport” content=”width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=no;” />
each are different values I found recommended by google searching or SO searching, but none of the 'user-scalable=X' values seem to be working
I also tried comma delimiting the values instead of semicolon, no luck. Then I tried ONLY having the user-scalable
value present, still no luck.
UPDATE
Got this from Apple's site and it works:
<meta name="viewport" content="width=device-width, user-scalable=no" />
it turns out that the problem was the non-standard quotes because I had copied the meta tag from a website that was using them, whoops
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
这在 IOS 10.3.2 中运行良好,
谢谢 @arthur 和 @aleclarson
This works fine in IOS 10.3.2
thank you @arthur and @aleclarson
如前所述,该解决方案基本上在 2020 年末有效:
但缺点是,当您滚动时,您仍然可以捏合,然后就会卡住。
解决方案是禁用滚动。
但是,如果您仍然希望页面滚动怎么办?
您仍然可以将另一个
设置为
overflow:auto
:然后
As mentioned this solution basically works as of late 2020:
But the downside is that while you are scrolling you'd still be able to pinch and then it gets stuck.
The solution is to disable scrolling.
But, what if you still wanted the page to be scrolled?
You can still do it with another
<div>
set asoverflow:auto
:and then
在 Safari 9.0 及更高版本中,您可以在视口元标记中使用 shrink-to-fit ,如下所示
In Safari 9.0 and up you can use shrink-to-fit in viewport meta tag as shown below
有时
content
标签中的其他指令可能会扰乱苹果对如何布局页面的最佳猜测/启发,您只需禁用捏缩放即可。sometimes those other directives in the
content
tag can mess up Apple's best guess/heuristic at how to layout your page, all you need to disable pinch zoom is.我尝试了以上所有方法,但这对我在 IOS 设备上有效:
I tried all above things but this worked for me on IOS devices:
截至今天(2022 年 10 月),使用 iOS 14.8,我可以完全阻止双击缩放的唯一方法是:
即使这样:(
这显然不现实,但仅用于演示目的)在每种情况下都不够。事实证明,我处理过
click
的任何for元素,双击它都会导致几乎不可逆的放大,完全忽略touch-action
设置。但是,如果我在点击处理程序中调用preventDefault()
,它就不会缩放。因此,到目前为止,在document
级别执行此操作似乎就足够了,这样我就不必每次处理click
时都执行此操作。我不知道这可能会产生什么副作用,但我相信如果人们想到任何副作用,他们会插话的。
As of today (Oct. 2022) with iOS 14.8, the ONLY way I could completely prevent the double tap zoom was this:
Even this:
(which is obviously not realistic, but just for demonstration purposes) wasn't enough in every case. It turned out that any for element on which I had handled
click
, double tapping on it would cause a nearly irreversible zoom-in, completely ignoring thetouch-action
setting. But if I calledpreventDefault()
in the click handler, it would not zoom. So, doing this at thedocument
level so far seems to be enough, so that I don't have to do it every time I handleclick
.I have no idea what side effects this might have, but I'm sure folks will chime in if they think of any.
2023 更新:关于 Safari IOS 的辅助功能和当前状态
我们在 Safari iOS 中遇到了表单输入焦点自动缩放问题。解决这个问题的结果比我们预期的更加复杂。
下面引用@haltersweb:
如果辅助功能要求不影响您,则会受到 9 分处罚Google Lighthouse 中的可访问性要求违规 - 以及相应的 SEO 降级 - 会让您暂停一下 是的,Chrome Mobile 会受到影响,
我们有几种方法可以解决此问题 。测试并工作(感谢 Rick Strahl用于他的研究和博客文章):
font-size: 16px
或更大maximum-scale=1
。。尽管第二种解决方案听起来很有希望,而且我们不想更改输入的设计,但将字体大小设置为 16px 是解决此问题的适当方法 最好避免引入黑客行为,因为最终您将不得不维护多年,并且会带来意想不到的后果。
2023 Update: on accessibility and the current state of Safari IOS
We had an auto-zoom issue with an input focus in a form, in Safari iOS. Solving the issue turned out to be more complex that we expected.
Quoting @haltersweb below:
If the accessibility requirements don't sway you, the 9 point penalty incurred in Google Lighthouse for accessibility requirements violations — with the corresponding SEO downgrade – will give you some pause. Yes, Chrome Mobile is affected. Do not disable pinch zoom.
There are a couple of ways to deal with this issue that we tested and work (kudos to Rick Strahl for his research and blogpost):
font-size: 16px
or greater.maximum-scale=1
in the viewport meta tag selectively for iOS Safari.Although the second solution sounds promising and we didn't want to deal with changing the design of the inputs, getting the font-size to 16px is the appropriate way to deal with this. It is always better to abstain from introducing hacks that in the end, you'll have to maintain for many years, and that will come with unintended consequences.
我愚蠢地有一个包装 div,其宽度以像素为单位。其他浏览器似乎足够聪明来处理这个问题。当我将宽度转换为百分比值后,它在 Safari 移动设备上也能正常工作。很烦人。
到
I foolishly had a wrapper div which had a width measured in pixels. The other browsers seemed to be intelligent enough to deal with this. Once I had converted the width to a percentage value, it worked fine on Safari mobile as well. Very annoying.
to
为了符合 WAI WCAG 2.0 AA 辅助功能要求,您必须切勿禁用捏合缩放。 (WCAG 2.0:SC 1.4.4 调整文本大小 AA 级)。您可以在这里阅读更多相关信息:移动辅助功能:WCAG 2.0 和其他W3C/WAI 指南适用于移动设备,2.2 缩放/放大
In order to comply with WAI WCAG 2.0 AA accessibility requirements you must never disable pinch zoom. (WCAG 2.0: SC 1.4.4 Resize text Level AA). You can read more about it here: Mobile Accessibility: How WCAG 2.0 and Other W3C/WAI Guidelines Apply to Mobile, 2.2 Zoom/Magnification
这个应该可以在 iPhone 等设备上运行。
This one should be working on iphone etc.
编辑:iOS 10 之后可能无法工作,请参阅下面基于
touch-action
的解决方案。您的代码将属性双引号显示为精美的双引号。如果您的实际源代码中存在花哨的引号,我猜这就是问题所在。
这对我在 iOS 4.2 中的 Mobile Safari 上有效。
Edit: may not work after iOS 10, please see
touch-action
based solution below.Your code is displaying attribute double quotes as fancy double quotes. If the fancy quotes are present in your actual source code I would guess that is the problem.
This works for me on Mobile Safari in iOS 4.2.
对于寻求 iOS 10 解决方案的人来说,
user-scaleable=no
在 iOS 10 的 Safari 中被禁用。原因是 Apple 试图通过允许人们缩放网页来提高可访问性。摘自发行说明:
据我了解,我们运气不好。
For the people looking for an iOS 10 solution,
user-scaleable=no
is disabled in Safari for iOS 10. The reason is that Apple is trying to improve accessibility by allowing people to zoom on web pages.From release notes:
So as far as I understand, we are sh** out of luck.
@mattis 是正确的,iOS 10 Safari 不允许您使用用户可缩放属性禁用捏合缩放。但是,我让它在“gesturestart”事件上禁用使用preventDefault。我只在 iOS 10.0.2 的 Safari 上验证了这一点。
@mattis is correct that iOS 10 Safari won't allow you to disable pinch to zoom with the user-scalable attribute. However, I got it to disable using preventDefault on the 'gesturestart' event. I've only verified this on Safari in iOS 10.0.2.
使用 CSS
touch-action
属性是最优雅的解决方案。在 iOS 13.5 和 iOS 14 上进行了测试。要禁用捏合缩放手势并双击缩放:
如果您的应用程序也不需要平移(即滚动),请使用以下命令:
Using the CSS
touch-action
property is the most elegant solution. Tested on iOS 13.5 and iOS 14.To disable pinch zoom gestures and and double-tap to zoom:
If your app also has no need for panning, i.e. scrolling, use this:
对于 iphone safari 直至 iOS 10“视口”不是一个解决方案,我不喜欢这种方式,但我使用了这个 javascript 代码,它对我有帮助
for iphones safari up to iOS 10 "viewport" is not a solution, i don't like this way, but i have used this javascript code and it helped me
我使用以下代码让它在 iOS 12 中工作:
使用第一个 if 语句,我确保它只会在 iOS 环境中执行(如果它在 Android 中执行,滚动行为将被破坏)。另请注意将
passive
选项设置为false
。I got it working in iOS 12 with the following code:
With the first if statement I ensure it will only execute in iOS environments (if it executes in Android the scroll behivour will get broken). Also, note the
passive
option set tofalse
.我通过将以下内容添加到 HTML 标头来设法阻止此行为。这适用于移动设备,因为桌面浏览器在使用鼠标滚轮时支持缩放。对于桌面浏览器来说这不是什么大问题,但考虑到这一点很重要。
CSS 样式表遵循以下规则
I managed to stop this behavior by adding the following to the HTML header. This works on mobile devices, as desktop browsers support zooming when using the mouse wheel. It's not a big deal on desktop browsers but it's important to take this into account.
and the following rule to the CSS stylesheet
实际上,Apple 在最新的 iOS 版本上禁用了 user-scalable=no 。
我尝试作为指导,这种方法可以工作:
Actually Apple disabled user-scalable=no on latest iOS versions.
I tried as guideline and this way can work:
这在 iOS 10 上不再起作用。Apple 删除了该功能。
现在你无法在 iOS 上禁用 Zoom 网站,除非你制作了一个大平台应用程序。
This no longer works on iOS 10. Apple removed the feature.
There is no way yo can disable zoom website on iOS now, unless you make gross platform app.
尝试将以下内容添加到您的 head 标签中:
另外
最后,作为样式属性或在 css 文件中,为基于 webkit 的浏览器添加以下文本:
Try adding the following to your head-tag:
additionally
Finally, either as a style-attribute or in your css file, add the following text for webkit-based Browsers: