使用maximum-scale 和minimum-scale 来停止缩放并不能真正起作用,因为这样会剥夺用户缩放的能力。这真是个坏主意,因为它会让眼睛不好的用户对您的网站不缩放而生气,而其他网站却...
我尝试了超时和各种花哨的 javascript,然后我发现了这个:
https://github.com/scottjehl/iOS-Orientationchange-Fix
通过这个相关问题:How do I reset the scale/zoom of a web app on an orientation change on the iPhone?
在那篇帖子中,Andrew Ashbacher 发布了一个指向 Scott Jehl 编写的代码的链接:
/*! A fix for the iOS orientationchange zoom bug. Script by @scottjehl, rebound by @wilto.MIT License.*/(function(m){if(!(/iPhone|iPad|iPod/.test(navigator.platform)&&navigator.userAgent.indexOf("AppleWebKit")>-1)){return}var l=m.document;if(!l.querySelector){return}var n=l.querySelector("meta[name=viewport]"),a=n&&n.getAttribute("content"),k=a+",maximum-scale=1",d=a+",maximum-scale=10",g=true,j,i,h,c;if(!n){return}function f(){n.setAttribute("content",d);g=true}function b(){n.setAttribute("content",k);g=false}function e(o){c=o.accelerationIncludingGravity;j=Math.abs(c.x);i=Math.abs(c.y);h=Math.abs(c.z);if(!m.orientation&&(j>7||((h>6&&i<8||h<8&&i>6)&&j>5))){if(g){b()}}else{if(!g){f()}}}m.addEventListener("orientationchange",f,false);m.addEventListener("devicemotion",e,false)})(this);
这是一个很好地包含在 IIFE 中的解决方案,因此您不必担心名称空间问题。
只需将其放入您的脚本(如果您使用 jQuery,则不要放入 document.ready())和中提琴!
它所做的只是禁用devicemotion 事件的缩放,这些事件表明orientationchange 即将发生。这是我见过的最好的解决方案,因为它确实有效并且不会禁用缩放。
编辑:这种方法并不总是可靠的,尤其是当您以一定角度握住 ipad 时。另外,我认为这个活动不适用于第 1 代 ipad