How can I reset the zoom after form submission in mobile Safari, while maintaining user scalability afterwards?

最后都变了- 提交于 2019-12-21 04:38:04

问题


When a user logs in to my app from an iPhone/iPad, Safari (helpfully) zooms in while the user is filling out the username/password fields. But when the form is submitted and we log them in, we don't reload the page (this is a single page application), so the zoom is never reset. So the app is always started at a zoomed in scale.

I have looked at Jeremy Keith's solution, which successfully resets the zoom, but also prevents future scaling/zooming by the user, because he sets the maximum-scale of the viewport.

Like this:

var viewportmeta = document.querySelector('meta[name="viewport"]');

if (viewportmeta) {
    viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0';
}

Has anyone seen a nice solution for reseting this after a form submission, without freezing up the viewport afterwards?


回答1:


What I found to work is a little hacky, but seems effective. Basically, on form submission, I'm setting the maximum-scale, and then immediately removing it. Hope this helps someone.

element.on('submit', function(event) {
    if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
      var viewportmeta = document.querySelector('meta[name="viewport"]');
      if (viewportmeta) {
        viewportmeta.setAttribute('content', 'width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0');
        viewportmeta.setAttribute('content', 'width=device-width, minimum-scale=1.0, initial-scale=1.0');
      }
   }
}


来源:https://stackoverflow.com/questions/29927680/how-can-i-reset-the-zoom-after-form-submission-in-mobile-safari-while-maintaini

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!