我制作了一个包含 <input>
标签以及 type="text"
属性的 HTML 页面。当我在 iPhone 上使用 Safari 点击它时,页面会变大(自动缩放)。有人知道如何禁用此功能吗?
我制作了一个包含 <input>
标签以及 type="text"
属性的 HTML 页面。当我在 iPhone 上使用 Safari 点击它时,页面会变大(自动缩放)。有人知道如何禁用此功能吗?
经过一段时间的尝试,我想出了这个解决方案。
// set font-size to 16px to prevent zoom
input.addEventListener("mousedown", function (e) {
e.target.style.fontSize = "16px";
});
// change font-size back to its initial value so the design will not break
input.addEventListener("focus", function (e) {
e.target.style.fontSize = "";
});
我也用jQuery实现了这个功能:
$('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'));
});
使用 (hover:none) 和 (pointer:coarse)
来针对所有触摸屏设备:
这里有一些答案通过部署JavaScript或jQuery来解决问题。
但是,完全可以(也确实可以)用CSS来控制条件性的字体呈现等问题。
Safari Mobile 要求(理由充足)任何表单元素在被交互时必须具有最小字体大小 16px
(或视觉等效大小)。
我们承诺将这种深思熟虑的用户体验应用于所有触摸屏浏览器上。
因此我们可以采用以下方法:
@media only screen and (hover: none) and (pointer: coarse) {
input,
select,
textarea {
font-size: 11px;
}
input:focus,
select:focus,
textarea:focus {
font-size: 16px;
}
}
当使用触摸屏设备时,如果有任何交互式表单元素被聚焦,该表单元素的font-size
将暂时设置为16px
。
这会禁用iOS Safari Mobile的auto-zoom
功能。
所有设备上的用户启动的缩放操作都不受影响,也永远不会被禁用。
阅读了这里几乎每一行代码并测试了各种解决方案,感谢所有提供解决方案的人,以下是我得出的、经过测试并在我的 iPhone 7 iOS 10.x 上有效的解决方案:
@media screen and (-webkit-min-device-pixel-ratio:0) {
input[type="email"]:hover,
input[type="number"]:hover,
input[type="search"]:hover,
input[type="text"]:hover,
input[type="tel"]:hover,
input[type="url"]:hover,
input[type="password"]:hover,
textarea:hover,
select:hover{font-size: initial;}
}
@media (min-width: 768px) {
input[type="email"]:hover,
input[type="number"]:hover,
input[type="search"]:hover,
input[type="text"]:hover,
input[type="tel"]:hover,
input[type="url"]:hover,
input[type="password"]:hover,
textarea:hover,
select:hover{font-size: inherit;}
}
然而,它也有一些缺点,明显的是“跳动”,即由于“悬停”和“聚焦”状态之间快速字体大小变化所导致的效果 - 以及对性能的重新绘制影响。
我曾经需要为一所荷兰大学的网站修复自动缩放到表单控件的问题(使用了15px的表单控件)。我想出了以下要求:
这是我目前想出的方案:
/*
NOTE: This code overrides the viewport settings, an improvement would be
to 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 focus
if ( tn != "SELECT"
&& tn != "TEXTAREA"
&& (tn != "INPUT" || dont_disable_for.indexOf(evt.target.getAttribute("type")) > -1)
)
return;
// disable zoom
setViewport("width=device-width, initial-scale=1.0, user-scalable=0");
}
// NOTE: for now assuming this focusIn is caused by user interaction
function onFocusIn(evt)
{
// reenable zoom
setViewport("width=device-width, initial-scale=1.0, user-scalable=1");
}
// add or update the <meta name="viewport"> element
function 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);
}
}
一些注意事项:
import { Directive, ElementRef, HostListener } from '@angular/core';
const MINIMAL_FONT_SIZE_BEFORE_ZOOMING_IN_PX = 16;
@Directive({ selector: '[noZoomiOS]' })
export class NoZoomiOSDirective {
constructor(private el: ElementRef) {}
@HostListener('focus')
onFocus() {
this.setFontSize('');
}
@HostListener('mousedown')
onMouseDown() {
this.setFontSize(`${MINIMAL_FONT_SIZE_BEFORE_ZOOMING_IN_PX}px`);
}
private setFontSize(size: string) {
const { fontSize: currentInputFontSize } = window.getComputedStyle(this.el.nativeElement, null);
if (MINIMAL_FONT_SIZE_BEFORE_ZOOMING_IN_PX <= +currentInputFontSize.match(/\d+/)) {
return;
}
const iOS = navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform);
iOS
&& (this.el.nativeElement.style.fontSize = size);
}
}
*.module.ts
之后,您可以像这样使用它:<input noZoomiOS >
。太棒了,这里有很多与JavaScript和视口有关的答案,只有另外一个提到了 text-size-adjust
,我认为这是最好的解决方案。
你只需要将它设置为none
。
添加以下CSS:
* {
-webkit-text-size-adjust: none;
text-size-adjust: none;
}
text-size-adjust
仍被视为实验性质,但当前移动版Chrome和Safari已支持该属性。请务必查看caniuse.com。 - rockyinput[type="text"]:focus,
textarea:focus {
-webkit-text-size-adjust: 100%;
}
.form-control {
font-size: 16px;
}