Prevent iPhone from zooming form?

2020-05-11 10:46发布

The code:

<select>
    <option value="1">Home</option>
    <option value="2">About</option>
    <option value="3">Services</option>
    <option value="4">Contact</option>
</select>

When I touch select, the iPhone zooms in that element (and does not zoom out after deselecting).

How can I prevent this? Or zoom back out? I can't use user-scalable=no because I actually need that functionality. It's for iPhone, select menu.

12条回答
疯言疯语
2楼-- · 2020-05-11 10:53

So here is the final fix which works well for me.


    @media screen and (-webkit-min-device-pixel-ratio:0) { 
      select,
      textarea,
      input {
        font-size: 16px !important;
      }
    }

查看更多
地球回转人心会变
3楼-- · 2020-05-11 10:54

Use maximum-scale=1 instead of user-scalable=no to prevent the form zooming issue without breaking the user’s ability to pinch zoom.

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

查看更多
成全新的幸福
4楼-- · 2020-05-11 10:56

This might be helpful to look at:

Disable Auto Zoom in Input "Text" tag - Safari on iPhone

You'd basically need to capture the event of tapping on a form element, then not run the default iOS action of zooming in, but still allowing it to zoom for the rest of the page.

Edit:

The link mentions,

2) You can dynamically change the META viewport tag using javascript (see Enable/disable zoom on iPhone safari with Javascript?)

To elaborate:

  1. Viewport meta tag is set to allow zooming
  2. User taps on form element, changes meta tag to disable zooming
  3. Upon pressing done, viewport is changed to allow zoom

And if you can't change the tag when clicking on a form element, put a div that mimics the form element that when you press it, it changes the tag, then calls the input.

查看更多
Lonely孤独者°
5楼-- · 2020-05-11 10:57

Just found a simple fix if you're using Bootstrap:

As mentioned in w3s: You can quickly size labels and form controls within a Horizontal form by adding .form-group-lg to the element.

<form class="form-horizontal">
  <div class="form-group form-group-lg">
    <label class="control-label">Large Label</label>
    <div>
      <input class="form-control" type="text">
    </div>
  </div>

See second example on this page: http://www.w3schools.com/bootstrap/bootstrap_forms_sizing.asp

Tested it in Safari and Chrome on an iPhone SE and it works like a charm!

查看更多
我想做一个坏孩纸
6楼-- · 2020-05-11 10:59

The most up voted answer to set the font-size does not work for me. Using javascript to identify the client together with the meta tags in the answers here, we can prevent the zooming behavior of iPhone on input focus while otherwise keeping the zooming functionality intact.

$(document).ready(function ()
{
    if (/iPhone/.test(navigator.userAgent) && !window.MSStream)
    {
        $(document).on("focus", "input, textarea, select", function()
        {
            $('meta[name=viewport]').remove();
            $('head').append('<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">');
        });

        $(document).on("blur", "input, textarea, select", function()
        {
            $('meta[name=viewport]').remove();
            $('head').append('<meta name="viewport" content="width=device-width, initial-scale=1">');
        });
    }
});

It seems like we have to replace the meta tag with new values on the blur-event, just to remove it does not seem to trigger an updated behavior.

Note that the UI is still initializing the zoom, but it quickly zooms back out again. I believe this is acceptable and iPhone users must already be accustomed to that the browser is having some dynamic zooming going on anyway in applicable scenarios.

查看更多
7楼-- · 2020-05-11 11:00

This can be prevented by setting font-size:16px to all input fields.

查看更多
登录 后发表回答