ChatGPT解决这个技术问题 Extra ChatGPT

How do you disable viewport zooming on Mobile Safari?

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

If you're making a game, then possibly controlling zooming is valid. However in nearly all other cases it should be strongly discouraged. Unfortunately it has become standard practice for a lot of mobile developers. If a user wants to zoom in to be able to read text more easily etc. preventing them from doing so isn't very nice.
We are disabling zoom on a mobile web based application. You can't zoom in Native iOS apps and it's not required in our web application. If your site or app is mobile optimised then your users won't need to zoom. There are always use cases for disabling zoom. It doesn't have to always be one way or the other.
Yea I'm not following the line of logic that disabling zoom on a mobile optimised site is a bad thing. What's far worse is having the viewport accidentally pan around because the screen is picking up accidental pan and zoom input on a site that doesn't require you to zoom anyway. Realistically if your users are needing to zoom into content on your mobile optimised site then the problem is the design, not the lack of zoom.
@NathanHornby: The problem of disabling zoom is a lack of respect of user's preferences. Different users prefers different text sizes, younger audience with good eyesight might prefer to see more content, while audience with poor eyesight can't use anything that doesn't have giant text. Other people have Parkinsons but still have good eyesight, so they prefer extra large buttons but don't benefit from large text in general.
@NathanHornby It doesn't mean the designer has done something wrong necessarily. It simply means the user, for whatever reason, wants to zoom in. Pinch-zoom is a standard feature of all touch-screen phones. Any user who owns such a phone, knows how to use it. I know it's an old question, but I still am constantly frustrated by know-all developers who insist on breaking the functionality of my phone because they think it makes their designs look better.

B
BoltClock

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.

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

it was the fancy quotes, I had copied the tag from a website without noticing, thanks for pointing that out!
Perhaps this is at the long tail here but it might be worth pointing out that this needs to be applied at the "top level" page. If you have this meta tag applied to an iframe, it won't work unless the meta tag is also applied to the top-most page.
Why anyone would ever create a piece of software that creates fancy quotes is beyond me.
@Traubenfuchs: Typography.
This is a rather old answer and coming to the problem I had read that if you set user-scalable to no this would cause accessibility problems. As of iOS 10 it works fine, allowing the user to zoom if he wishes, but but not zooming the input box otherwise. No need to set large font sizes either.
M
Mattis

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:

To improve accessibility on websites in Safari, users can now pinch-to-zoom even when a website sets user-scalable=no in the viewport.

So as far as I understand, we are sh** out of luck.


What a terrible decision by Apple. Using a spinner with "-" and "+" buttons to decrement/increment a number now repeatedly zooms the page in and out on iOS Safari. The presses are being interpreted as double-tap to zoom, with no way to disable that. iOS Chrome working perfectly. Frustrating.
Although I'm screwed of that decision but I'm really happy from apple as a user as I wanted to force this feature in a lot of websites that were using small elements inside.
the world is going to hell
Oh jeez, who would've guessed? Another ridiculous choice made by Apple
Leave it to Apple to force an accessibility "feature" on everyone instead of just adding an option for it in the Accessibility settings...
D
David Zorychta

@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.

document.addEventListener('gesturestart', function (e) {
    e.preventDefault();
});

On iOS 10, I found that this worked, but if you scroll the page and then pinch-zoom while still scrolling, it allows the zoom. Then you find yourself stuck at the new zoom level until you scroll again. So this doesn't look like a good solution unless your page body is not scrollable. :(
One caveat: the user can still double tap the screen which will/can zoom and is not caught by this.
Still sometimes allows zooming by double-tapping the screen. :(
What is the "double tap" equivalent to gesturestart? dblclick?
Another note, if you want to disable the double tap aspect of zooming. Mobile Safari also supports 'touch-action: manipulation' (falls under 'basic support', which disables double tap events. Documentation here: developer.mozilla.org/en-US/docs/Web/CSS/touch-action
F
Feross

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:

body {
  touch-action: pan-x pan-y;
}

If your app also has no need for panning, i.e. scrolling, use this:

body {
  touch-action: none;
}

the only way I got it working on IOS 13.6.1 thanks!
This is the current answer.
NB does not work on iOS 12 and below (which is still quite popular).
I tried no effect, can still zoom. iPhone6sp IOS 13.6(17G68).
touch-action: pan-x pan-y is unfortunately janky and broken. If you pinch repeatedly it will still zoom about 5% of the time.
A
Arthur Tsidkilov

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

 document.addEventListener('touchmove', function(event) {
    event = event.originalEvent || event;
    if(event.scale > 1) {
      event.preventDefault();
    }
  }, false);

It should be event.scale !== 1
@aleclarson event.scale > 1 include condition event.scale !== 1
@aleclarson for prevent zooming on Mobile Safari it is enough, i wrote that i don't like this way, normally for it have to be used viewport, but on IOS iphone 6 and higher it is not working (i think your event.scale !== 1 should be more correct, but all this not correct, it is kind of hacking)
@aleclarson Seems using !== breaks the native browser in Android 4.4; event.scale is undefined.
@JamesPizzurro Yeah, you can use event.scale !== undefined && event.scale !== 1
J
José Antonio Postigo

I got it working in iOS 12 with the following code:

if (/iPad|iPhone|iPod/.test(navigator.userAgent)) {
  window.document.addEventListener('touchmove', e => {
    if(e.scale !== 1) {
      e.preventDefault();
    }
  }, {passive: false});
}

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 to false.


Not work on my iOS 12.3.1, here is test link: https://output.jsbin.com/liqiraj
This fixed it for me in my Ionic v1 PWA on iOS 14.4!
This works to prevent page zoom but breaks custom gesture handling.
P
Pham Duc Toan

Actually Apple disabled user-scalable=no on latest iOS versions. I tried as guideline and this way can work:

body {
  touch-action: pan-x pan-y;
}

I tried it on IOS 15, not working!
a
angry kiwi
user-scalable=0

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.


Such a bummer... Any idea why they removed this?
@smt they don't want web experience to compete with app experience on the app store.
@marvindanig yes... since they take 99$ fee to produce an app that could else easily be made an more accessible web page application for all platform. Also of Apple cannot like a "web experience", if it means users + devs can avoid the walled garden. Its all about power and money of Apple :(
It is actually possible to get this working on newer iOS versions. See my answer: stackoverflow.com/a/62165035
i
imelgrat

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

html { -webkit-text-size-adjust: none; touch-action: manipulation; }


"touch-action" do that work - thx
L
Lorenz Lo Sauer

Try adding the following to your head-tag:

<meta name="viewport" content="width=device-width, initial-scale=1.0, 
minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">

additionally

<meta name="HandheldFriendly" content="true">

Finally, either as a style-attribute or in your css file, add the following text for webkit-based Browsers:

html {
    -webkit-text-size-adjust: none
}

N
Nikhil Gowda

This works fine in IOS 10.3.2

    document.addEventListener('touchmove', function(event) {
        event = event.originalEvent || event;
        if (event.scale !== 1) {
           event.preventDefault();
        }
    }, false);

thank you @arthur and @aleclarson


iOS 13 change false to {passive: false}
u
ursuleacv

In Safari 9.0 and up you can use shrink-to-fit in viewport meta tag as shown below

<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">

A
Alex Borsody

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.

<meta name="viewport" content="user-scalable=no" />

a
amin

As mentioned this solution basically works as of late 2020:

document.addEventListener(
    'gesturestart', (e) => e.preventDefault()
);

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.

body {
    overflow: hidden;
}

But, what if you still wanted the page to be scrolled? You can still do it with another <div> set as overflow:auto:

<body>
    <div id='app'></div>
</div>

and then

body {
    overflow: hidden;
}

 #app {
     -webkit-overflow-scrolling: touch;
      height: 100vh;
      height: -webkit-fill-available;
      overflow: auto;
 }

h
haltersweb

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


This is not an answer.. and many clients yet ask to block zoom.. so I can't take this as a general rule
Accessibility zoom is not the page's (neither any app for that matter) responsibility. It is the operating system's responsibility to provide an accessible zooming tool. Every OS nowadays provides such feature, which applies as a screen zoom, and it should not interfere with the page's zooming itself, because this is not intended for accessibility.
There are always valid use cases for stuff like this. For some reason, pretty much all web literature seems to assume that the web is only for making blogs. Just as there are valid use cases for JavaScript's eval(), so are there for disabling zooming. I am using it for a web-app that is used in combination with a Bluetooth scanner, to prevent the page zooming when a barcode is scanned.
I completely agree with the aims of the accessibility recommendations, but there's no question that there are times when zoom is not desired or needed, or may even break the user experience. Plus, you know, clients.
I'd say that in a html/javascript app, those guidelines do not apply. After all, would a native app let you zoom in and out?
W
Welshboy

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.

.page{width: 960px;}

to

.page{width:93.75%}

<div id="divPage" class="page">
</div>

C
Cihan Zengin

This one should be working on iphone etc.

<meta name="viewport" content="width=device-width, initial-scale=1 initial-scale=1.0, maximum-scale=1.0, user-scalable=no">