我制作了一个HTML页面,其中包含<input>
标签和type="text"
。当我在iPhone上使用Safari点击它时,页面变大(自动缩放)。有人知道如何禁用它吗?
如果font-size小于16px
,则浏览器将缩放,表单元素的默认字体大小为11px
(至少在Chrome和Safari中)。
另外,select
元素需要附加focus
伪类。
input[type="color"],
input[type="date"],
input[type="datetime"],
input[type="datetime-local"],
input[type="email"],
input[type="month"],
input[type="number"],
input[type="password"],
input[type="search"],
input[type="tel"],
input[type="text"],
input[type="time"],
input[type="url"],
input[type="week"],
select:focus,
textarea {
font-size: 16px;
}
没有必要使用上述所有内容,您可以设置所需的元素,例如:只需text
,number
和textarea
:
input[type='text'],
input[type='number'],
textarea {
font-size: 16px;
}
使输入元素继承父样式的替代解决方案:
body {
font-size: 16px;
}
input[type="text"] {
font-size: inherit;
}
将user-scalable = 0添加到viewport meta,如下所示
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
为我工作:)
在iOS 7上运行的Javascript hack。这是基于@dlo的答案,但mouseover和mouseout事件被touchstart和touchend事件取代。基本上,此脚本会在再次启用缩放之前添加半秒超时,以防止缩放。
$("input[type=text], textarea").on({ 'touchstart' : function() {
zoomDisable();
}});
$("input[type=text], textarea").on({ 'touchend' : function() {
setTimeout(zoomEnable, 500);
}});
function zoomDisable(){
$('head meta[name=viewport]').remove();
$('head').prepend('<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0" />');
}
function zoomEnable(){
$('head meta[name=viewport]').remove();
$('head').prepend('<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=1" />');
}
这对我有用:
input, textarea {
font-size: initial;
}
我使用了上面的Christina解决方案,但对引导程序进行了少量修改,并将另一条规则应用于台式计算机。 Bootstrap的默认字体大小为14px,导致缩放。对于Bootstrap中的“表单控件”,以下内容将其更改为16px,从而阻止了缩放。
@media screen and (-webkit-min-device-pixel-ratio:0) {
.form-control {
font-size: 16px;
}
}
对于非移动浏览器,返回14px。
@media (min-width: 768px) {
.form-control {
font-size: 14px;
}
}
我尝试使用.form-control:focus,它将它保持在14px,除了焦点,它将其更改为16px并且它没有修复iOS8的缩放问题。至少在使用iOS8的我的iPhone上,字体大小必须是16px才能让iPhone无法缩放页面。
我也用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'));
});
当然,如果这个16px
字体大小破坏了设计,则可能必须调整界面中的一些其他元素。
经过一段时间的尝试,我想出了这个解决方案
// 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 = "";
});
在“mousedown”上,它将输入的字体大小设置为16px。这将阻止缩放。在焦点事件上,它将字体大小更改回初始值。
与之前发布的解决方案不同,这将允许您将输入的字体大小设置为您想要的任何内容。
在阅读了几乎每一行并测试各种解决方案之后,感谢所有分享他们解决方案的人,我在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;}
}
然而,由于“悬停”编辑和“焦点”编辑状态之间出现快速字体大小变化,显然有一些“跳跃” - 重绘对性能的影响
顺便说一句,如果你使用Bootstrap,你可以使用这个变种:
.form-control {
font-size: 16px;
}
我必须将自动缩放“修复”为荷兰大学网站(在表单控件中使用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);
}
}
一些说明:
像:focus
这样的伪元素不像以前那样有效。从iOS 11开始,可以在主要样式之前添加简单的重置声明(假设您不使用较小的字体大小覆盖它们)。
/* Prevent zoom */
select, input, textarea {
font-size: 16px;
}
值得一提的是,对于像Tachyons.css这样的CSS库,很容易意外地覆盖你的字体大小。
例如,类:f5
相当于:fontSize: 1rem
,如果你将body字体比例保持为默认值,这很好。
但是:如果您选择字体大小类:f6
,这将相当于向上的小显示屏上的fontSize: .875rem
。在这种情况下,您需要更具体地了解重置声明:
/* Prevent zoom */
select, input, textarea {
font-size: 16px!important;
}
@media screen and (min-width: 30em) {
/* not small */
}
@media screen and (-webkit-min-device-pixel-ratio:0) {
select:focus,
textarea:focus,
input:focus {
font-size: 16px;
background: #eee;
}
}
@media screen and (-webkit-min-device-pixel-ratio:0) {
select,
textarea,
input {
font-size: 16px;
}
}
我添加了一个背景,因为IOS在select上没有添加背景。
我看到这里的人用JavaScript或视口功能做一些奇怪的事情并关闭所有手动缩放设备。在我看来,这不应该是一个解决方案。添加此CSS代码段将关闭iOS中的自动缩放,而不会将字体大小更改为固定数字(如16px)。
默认情况下,我在输入字段使用93.8%(15px)字体大小,通过添加我的CSS片段,它保持在93.8%。无需更改为16px或使其成为固定数字。
input[type="text"]:focus,
textarea:focus {
-webkit-text-size-adjust: 100%;
}
将字体大小(对于输入字段)设置为等于正文的字体大小,似乎是阻止浏览器缩小或缩小的原因。我建议使用font-size: 1rem
作为更优雅的解决方案。
由于自动放大(没有缩小)在iPhone上仍然很烦人,这里有一个基于dlo建议使用焦点/模糊的JavaScript。
一旦文本输入被聚焦并且在输入被保留时重新启用,则缩放被禁用。
注意:某些用户可能无法在小文本输入中修改编辑文本!因此,我个人更喜欢在编辑期间更改输入的文本大小(请参阅下面的代码)。
<script type="text/javascript">
<!--
function attachEvent(element, evtId, handler) {
if (element.addEventListener) {
element.addEventListener(evtId, handler, false);
} else if (element.attachEvent) {
var ieEvtId = "on"+evtId;
element.attachEvent(ieEvtId, handler);
} else {
var legEvtId = "on"+evtId;
element[legEvtId] = handler;
}
}
function onBeforeZoom(evt) {
var viewportmeta = document.querySelector('meta[name="viewport"]');
if (viewportmeta) {
viewportmeta.content = "user-scalable=0";
}
}
function onAfterZoom(evt) {
var viewportmeta = document.querySelector('meta[name="viewport"]');
if (viewportmeta) {
viewportmeta.content = "width=device-width, user-scalable=1";
}
}
function disableZoom() {
// Search all relevant input elements and attach zoom-events
var inputs = document.getElementsByTagName("input");
for (var i=0; i<inputs.length; i++) {
attachEvent(inputs[i], "focus", onBeforeZoom);
attachEvent(inputs[i], "blur", onAfterZoom);
}
}
if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
attachEvent(window, "load", disableZoom);
}
// -->
</script>
以下代码将在元素具有焦点期间将输入的文本大小更改为16像素(计算,即以当前缩放大小计算)。因此iPhone不会自动放大。
注意:缩放系数是根据window.innerWidth和iPhone的320像素显示计算的。这仅适用于纵向模式下的iPhone。
<script type="text/javascript">
<!--
function attachEvent(element, evtId, handler) {
if (element.addEventListener) {
element.addEventListener(evtId, handler, false);
} else if (element.attachEvent) {
var ieEvtId = "on"+evtId;
element.attachEvent(ieEvtId, handler);
} else {
var legEvtId = "on"+evtId;
element[legEvtId] = handler;
}
}
function getSender(evt, local) {
if (!evt) {
evt = window.event;
}
var sender;
if (evt.srcElement) {
sender = evt.srcElement;
} else {
sender = local;
}
return sender;
}
function onBeforeZoom(evt) {
var zoom = 320 / window.innerWidth;
var element = getSender(evt);
element.style.fontSize = Math.ceil(16 / zoom) + "px";
}
function onAfterZoom(evt) {
var element = getSender(evt);
element.style.fontSize = "";
}
function disableZoom() {
// Search all relevant input elements and attach zoom-events
var inputs = document.getElementsByTagName("input");
for (var i=0; i<inputs.length; i++) {
attachEvent(inputs[i], "focus", onBeforeZoom);
attachEvent(inputs[i], "blur", onAfterZoom);
}
}
if (navigator.userAgent.match(/iPhone/i)) {
attachEvent(window, "load", disableZoom);
}
// -->
</script>
我花了一段时间才找到它,但这是我发现的最好的代码...... http://nerd.vasilis.nl/prevent-ios-from-zooming-onfocus/
var $viewportMeta = $('meta[name="viewport"]');
$('input, select, textarea').bind('focus blur', function(event) {
$viewportMeta.attr('content', 'width=device-width,initial-scale=1,maximum-scale=' + (event.type == 'blur' ? 10 : 1));
});
基于Stephen Walsh's answer ...此代码可以在不改变焦点输入的字体大小(看起来很蹩脚)的情况下工作,而且它仍适用于FastClick,我建议添加到所有移动网站以帮助带来“活泼”。调整“视口宽度”以满足您的需求。
// disable autozoom when input is focused
var $viewportMeta = $('head > meta[name="viewport"]');
$('input, select, textarea').bind('touchend', function(event) {
$viewportMeta.attr('content', 'width=640, user-scalable=0');
setTimeout(function(){ $viewportMeta.attr('content', 'width=640, user-scalable=1'); }, 1)
});
关于将font-size设置为16px的最佳答案的评论问这是一个解决方案,如果你想要更大/更小的字体怎么办。
我不知道你们所有人,但是使用px进行字体大小不是最好的方法,你应该使用em。
我在响应式网站上遇到了这个问题,我的文本字段大于16像素。我将表单容器设置为2rem,输入字段设置为1.4em。在我的移动查询中,我根据视口更改html字体大小。由于默认html为10,因此我的输入字段在桌面上计算为28px
要删除自动缩放,我必须将输入更改为1.6em。这使我的字体大小增加到32px。略高,几乎不引人注意。在我的iPhone 4和5上,我将html字体大小更改为15px(纵向),然后更改为10px(横向)。它显示该像素大小的最佳点是48px,这就是我从1.4em(42px)改为1.6em(48px)的原因。
您需要做的是找到font-size上的最佳位置,然后将其转换为rem / em大小。
这是我在我的一个项目中使用的hack:
select {
font-size: 2.6rem; // 1rem = 10px
...
transform-origin: ... ...;
transform: scale(0.5) ...;
}
结束了我想要的初始样式和比例,但没有放大焦点。
您可以:而不是简单地将字体大小设置为16px:
scale()
CSS transform和负边距将输入字段缩小到正确的大小。例如,假设您的输入字段最初的样式为:
input[type="text"] {
border-radius: 5px;
font-size: 12px;
line-height: 20px;
padding: 5px;
width: 100%;
}
如果通过将所有尺寸增加16/12 = 133.33%来放大字段,然后使用scale()
减少12/16 = 75%,输入字段将具有正确的视觉尺寸(和字体大小),并且将不会有缩放在焦点上。
由于scale()
仅影响视觉大小,因此您还需要添加负边距以减小字段的逻辑大小。
有了这个CSS:
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%;
}
输入字段的逻辑字体大小为16px,同时显示12px文本。
我有一篇博文,其中我将详细介绍一下,并将此示例作为可查看的HTML: No input zoom in Safari on iPhone, the pixel perfect way
在Angular中,您可以使用指令来防止关注IOS设备。没有元标记来保护可访问性。
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);
}
}
在<input noZoomiOS >
中声明后,你可以像使用*.module.ts
一样使用它
请不要使用Javascript或黑客来使其工作。这将影响您在网络上的项目得分。
这样就可以了:
input, input:active, input:focus, input:focus-within, input:hover, input:visited {
font-size: 16px!important;
}
如果您的网站设计适合移动设备,则可以决定不允许扩展。
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
这解决了您的移动页面或表单将“浮动”的问题。
这是工作!!!我完成了我的搜索旅程!
<meta name="viewport" content="width=640px, initial-scale=.5, maximum-scale=.5" />
在iPhone OS6,Android 2.3.3仿真器上测试
我有一个固定宽度为640px的移动网站,我正面对焦点的自动缩放。
我尝试了大量的挫败,但没有一个在iPhone和Android上都有效!
现在对我来说可以禁用缩放,因为该网站是移动优先设计!
这是我找到它的地方:How to do viewport sizing and scaling for cross browser support?
您可以阻止Safari在用户输入期间自动放大文本字段,而不会禁用用户捏缩放的功能。只需添加maximum-scale=1
,但省略其他答案中建议的用户比例属性。
如果你在一个图层中有一个“浮动”(如果放大),这可能会导致重要的UI元素移出屏幕,这是一个值得的选择。
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
总之,答案是:将表单元素的字体大小设置为至少16px
input[type='text'],textarea {font-size:1em;}
解决此问题的正确方法是将元视口更改为:
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>
我找不到干净的方式,但这里有一个黑客......
1)我注意到鼠标悬停事件发生在缩放之前,但是缩放发生在mousedown或焦点事件之前。
2)您可以使用javascript动态更改META视口标记(请参阅Enable/disable zoom on iPhone safari with Javascript?)
所以,尝试这个(在jquery中显示紧凑性):
$("input[type=text], textarea").mouseover(zoomDisable).mousedown(zoomEnable);
function zoomDisable(){
$('head meta[name=viewport]').remove();
$('head').prepend('<meta name="viewport" content="user-scalable=0" />');
}
function zoomEnable(){
$('head meta[name=viewport]').remove();
$('head').prepend('<meta name="viewport" content="user-scalable=1" />');
}
这绝对是一个黑客......可能存在鼠标悬停/下降并不总是捕获进入/退出的情况,但它在我的测试中运行良好并且是一个可靠的开始。
我最近(今天:D)不得不整合这种行为。为了不影响原始设计领域,包括组合,我选择在该领域的重点应用转换:
input[type="text"]:focus, input[type="password"]:focus,
textarea:focus, select:focus {
font-size: 16px;
}