简体   繁体   English

Javascript 媒体查询在 Safari/iOS 中不起作用

[英]Javascript media queries not working in safari / iOS

Hope someone can help.希望有人可以提供帮助。

The following lines of code work in everything except Safari.以下代码行适用于除 Safari 之外的所有应用程序。

 if(window.matchMedia('(resolution:320dpi)').matches){res = i;}
 if(window.matchMedia('screen and (resolution:320dpi)').matches){wkres = I;}

Has anyone else discovered how to get Safari to recognise / process these queries?有没有其他人发现如何让 Safari 识别/处理这些查询? Any and all help gratefully received.任何和所有的帮助都感激不尽。

Safari does not support resolution , though it looks like the relevant bug has been fixed so it'll come out at some point in a Safari release. Safari 不支持resolution ,但看起来相关错误已得到修复,因此它会在 Safari 版本中的某个时候出现。

It does support window.devicePixelRatio , though, which tells you the ratio of device pixels to CSS pixels (96ths of an inch).不过,它确实支持window.devicePixelRatio ,它告诉您设备像素与CSS 像素的比率(96 英寸)。

So for the resolution part of that, you can use something along these lines:因此,对于其中的解决方案部分,您可以使用以下内容:

const is320dpiOrMore = (window.devicePixelRatio * 96) >= 320;

As it says above, Safari does not support matchMedia resolution , but it supports the non-standard -webkit-device-pixel-ratio如上所述,Safari 不支持 matchMedia 分辨率,但它支持非标准的-webkit-device-pixel-ratio

/* A unit of "dppx" is implied: */
@media (-webkit-min-device-pixel-ratio: 2) { ... }
/* ... is equivalent to: */
@media (min-resolution: 2dppx) { ... }

/* Similarly: */
@media (-webkit-max-device-pixel-ratio: 2) { ... }
/* ... is equivalent to: */
@media (max-resolution: 2dppx) { ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM