gpt4 book ai didi

ios - Css - 使用 factorized css iphones 4 及更高版本的目标

转载 作者:太空宇宙 更新时间:2023-11-04 01:53:48 26 4
gpt4 key购买 nike

我希望只针对 iPhone 4、5、6 和 6 plus 设置一个 css 类我用了这个 article但想知道我是否不能将其分解。事实上,在我看来,前 2 个设备目标(最小宽度 320/最大最大宽度 480px 和 568px,我可以只使用最大的最大宽度并将它们融合到一个声明中。感觉我的代码不够干净,而是重复。

这是我当前的代码:

.example {
@media
//iphone 4
(min-device-width: 320px)
and (max-device-width: 480px)
and (-webkit-min-device-pixel-ratio: 2)
and (orientation: portrait),
//iphone 5
(min-device-width: 320px)
and (max-device-width: 568px)
and (-webkit-min-device-pixel-ratio: 2)
and (orientation: portrait),
//iphone 6
(min-device-width: 375px)
and (max-device-width: 667px)
and (-webkit-min-device-pixel-ratio: 2)
and (orientation: portrait),
//iphone 6+
(min-device-width: 414px)
and (max-device-width: 736px)
and (-webkit-min-device-pixel-ratio: 3)
and (orientation: portrait) {
color: yellow;
}
}

我可以将上面的代码转换/分解为以下代码吗?

.example {
@media
//mix iphone 4 and 5
(min-device-width: 320px)
and (max-device-width: 568pxpx)
and (-webkit-min-device-pixel-ratio: 2)
and (orientation: portrait),
//iphone 6
(min-device-width: 375px)
and (max-device-width: 667px)
and (-webkit-min-device-pixel-ratio: 2)
and (orientation: portrait),
//iphone 6+
(min-device-width: 414px)
and (max-device-width: 736px)
and (-webkit-min-device-pixel-ratio: 3)
and (orientation: portrait) {
color: red
}
}

这是否也意味着某些 Android 设备,例如 the Samsung Galaxy 3宽度与 iphone 4 (320px) 相同,我的类“示例”也会应用红色吗?

最佳答案

由于单独使用 CSS 安全地找到目标设备并不容易,headscript 标签中的这个脚本可能有助于检查用户代理和设备特定测量。

如果函数通过,它会添加一个类 iPhone456html 标记,您可以在 CSS 中使用它来定位那些 iPhone 型号上的特定元素。

请注意,您可能需要调整正则表达式,但该解决方案会给您一个良好的开端

/* rules for iPhone 4,5,6 */
.iPhone456 div.test {
color: red;
}
<head>
<script>
(function(d,s) {
/* Set html class if iPhone 4,5,6 */
if (!!navigator.userAgent.match(/iPhone/i)) {
var w = s.availWidth, h = s.availHeight;
if (
(w === 320 && (h === 480 || h === 568)) ||
(w === 375 && h === 667) ||
(w === 414 && h === 736)
) {
d.classList.add('iPhone456');
}
}
})(document.documentElement,screen);
</script>
</head>
<body>

<div class="test">Should be red on iPhone 4,5,6</div>

</body>

关于ios - Css - 使用 factorized css iphones 4 及更高版本的目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42955573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com