$('input[type=search]').on('focus', function(){// replace CSS font-size with 16px to disable auto zoom on iOS$(this).data('fontSize', $(this).css('font-size')).css('font-size', '16px');}).on('blur', function(){// put back the CSS font-size$(this).css('font-size', $(this).data('fontSize'));});
// set font-size to 16px to prevent zoominput.addEventListener("mousedown", function (e) {e.target.style.fontSize = "16px";});
// change font-size back to its initial value so the design will not breakinput.addEventListener("focus", function (e) {e.target.style.fontSize = "";});
/*NOTE: This code overrides the viewport settings, an improvement would beto take the original value and only add or change the user-scalable value*/
// optionally only activate for iOS (done because I havn't tested the effect under other OS/devices combinations such as Android)var iOS = navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform)if (iOS)preventZoomOnFocus();
function preventZoomOnFocus(){document.documentElement.addEventListener("touchstart", onTouchStart);document.documentElement.addEventListener("focusin", onFocusIn);}
let dont_disable_for = ["checkbox", "radio", "file", "button", "image", "submit", "reset", "hidden"];//let disable_for = ["text", "search", "password", "email", "tel", "url", "number", "date", "datetime-local", "month", "year", "color"];
function onTouchStart(evt){let tn = evt.target.tagName;
// No need to do anything if the initial target isn't a known element// which will cause a zoom upon receiving focusif ( tn != "SELECT"&& tn != "TEXTAREA"&& (tn != "INPUT" || dont_disable_for.indexOf(evt.target.getAttribute("type")) > -1))return;
// disable zoomsetViewport("width=device-width, initial-scale=1.0, user-scalable=0");}
// NOTE: for now assuming this focusIn is caused by user interactionfunction onFocusIn(evt){// reenable zoomsetViewport("width=device-width, initial-scale=1.0, user-scalable=1");}
// add or update the <meta name="viewport"> elementfunction setViewport(newvalue){let vpnode = document.documentElement.querySelector('head meta[name="viewport"]');if (vpnode)vpnode.setAttribute("content",newvalue);else{vpnode = document.createElement("meta");vpnode.setAttribute("name", "viewport");vpnode.setAttribute("content", newvalue);}}
input[type="text"] {/* enlarge by 16/12 = 133.33% */border-radius: 6.666666667px;font-size: 16px;line-height: 26.666666667px;padding: 6.666666667px;width: 133.333333333%;
/* scale down by 12/16 = 75% */transform: scale(0.75);transform-origin: left top;
/* remove extra white space */margin-bottom: -10px;margin-right: -33.333333333%;}
| maximum-scale | iOS: can zoom | iOS: no text field zoom | Android: can zoom || ------------------------- | ------------- | ----------------------- | ----------------- || yes | yes | yes | no || no | yes | no | yes || yes on iOS, no on Android | yes | yes | yes |
代码:
const addMaximumScaleToMetaViewport = () => {const el = document.querySelector('meta[name=viewport]');
if (el !== null) {let content = el.getAttribute('content');let re = /maximum\-scale=[0-9\.]+/g;
if (re.test(content)) {content = content.replace(re, 'maximum-scale=1.0');} else {content = [content, 'maximum-scale=1.0'].join(', ')}
el.setAttribute('content', content);}};
const disableIosTextFieldZoom = addMaximumScaleToMetaViewport;
// https://stackoverflow.com/questions/9038625/detect-if-device-is-ios/9039885#9039885const checkIsIOS = () =>/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
if (checkIsIOS()) {disableIosTextFieldZoom();}