我制作了一个HTML页面,其中有一个<input>标记,类型为“text”。当我在iPhone上使用Safari点击它时,页面会变大(自动缩放)。有人知道如何禁用此功能吗?


当前回答

请不要使用Javascript或黑客来使其工作。这将影响您在网络上的项目分数。

这将起到作用:

input, input:active, input:focus, input:focus-within, input:hover, input:visited {
    font-size: 16px!important;
}

其他回答

经过一段时间的尝试,我想出了这个解决方案

// 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。这将阻止缩放。在焦点事件中,它将字体大小更改回初始值。

与之前发布的解决方案不同,这将允许您将输入的字体大小设置为任意大小。

我用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的字体大小破坏了设计,则可能需要调整界面中的其他一些元素。

这在iOS Safari和Chrome上对我有效。对于输入选择器,可以将类或id设置为包含当前值。

@supports (-webkit-overflow-scrolling: touch) {
   input {
     font-size: 16px;
   }
}

令人惊讶的是,这里有很多javascript和视口的答案,只有一个提到了文本大小调整,这是我认为最好的解决方案。

您可以将此设置为无。

添加以下CSS:

* {
 -webkit-text-size-adjust: none;
  text-size-adjust: none;
}

使用(悬停:无)和(指针:粗略)以所有触摸屏设备为目标:

这里的许多答案都依赖于部署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;
  }
}

结果

当使用触摸屏设备时,当上面的任何交互式表单元素被聚焦时,该表单元素的字体大小被临时设置为16px。

这反过来会禁用iOS Safari Mobile自动缩放。

用户初始化的捏缩缩放在所有设备上都不受影响,并且永远不会被禁用。