iPhone 5的屏幕更长,我的网站移动视图无法适应。有哪些适用于iPhone 5的新响应式设计查询语句,我能否与现有的iPhone查询语句相结合?
我的当前媒体查询如下:
@media only screen and (max-device-width: 480px) {}
iPhone 5的屏幕更长,我的网站移动视图无法适应。有哪些适用于iPhone 5的新响应式设计查询语句,我能否与现有的iPhone查询语句相结合?
我的当前媒体查询如下:
@media only screen and (max-device-width: 480px) {}
另一个有用的媒体特性是device-aspect-ratio
。
需要注意的是,iPhone 5并没有16:9的宽高比。实际上是40:71。
iPhone < 5:
@media screen and (device-aspect-ratio: 2/3) {}
iPhone 5:
@media screen and (device-aspect-ratio: 40/71) {}
iPhone 6:
@media screen and (device-aspect-ratio: 375/667) {}
iPhone 6 Plus:
@media screen and (device-aspect-ratio: 16/9) {}
iPad:
@media screen and (device-aspect-ratio: 3/4) {}
and (-webkit-min-device-pixel-ratio: 2)
,以使其在PhoneGap webview中正常工作,正如@TaeKwonJoe所述。 - Mahendra Liya以下内容取自此博客:
@media only screen and (min-device-width: 560px) and (max-device-width: 1136px) and (-webkit-min-device-pixel-ratio: 2) {
/* iPhone 5 only */
}
请注意它是响应iPhone 5,而不是安装在该设备上的特定iOS版本。
要与您现有的版本合并,您应该能够使用逗号分隔它们:
@media only screen and (max-device-width: 480px), only screen and (min-device-width: 560px) and (max-device-width: 1136px) and (-webkit-min-device-pixel-ratio: 2) {
/* iPhone only */
}
NB: 上面的代码我没有测试过,但是我已经测试过逗号分隔的 @media
查询了,它们工作得很好。
请注意上述代码可能会影响其他共享相似比例的设备,例如Galaxy Nexus。这里有一种额外的方法,只能针对其中一个维度为640px(由于某些奇怪的显示像素异常而为560px),另一个维度在960px(iPhone <5)和1136px(iPhone 5)之间的设备。
@media
only screen and (max-device-width: 1136px) and (min-device-width: 960px) and (max-device-height: 640px) and (min-device-height: 560px),
only screen and (max-device-height: 1136px) and (min-device-height: 960px) and (max-device-width: 640px) and (min-device-width: 560px) {
/* iPhone only */
}
iPhone 5 and not to iOS 6
?它应该是:"iPhone 5 and not to iPhone 6"吗? - Sgnl所有上述列出的答案,使用max-device-width
或max-device-height
进行媒体查询的方法,都存在一个非常严重的 bug 问题:它们还会针对许多其他流行的移动设备进行匹配(可能是不需要的或从未经过测试的,或者将来可能上市的设备)。
这些查询将适用于任何屏幕更小的设备,很可能导致您的设计出现问题。
结合类似的特定于设备的媒体查询(例如HTC、三星、IPod、Nexus等),这种做法将启动一个定时炸弹。对于调试,这种想法会使您的 CSS 变得难以控制。您永远无法测试所有可能的设备。
请注意,唯一始终针对 iPhone5 而不是其他任何设备的媒体查询是:
/* iPhone 5 Retina regardless of IOS version */
@media (device-height : 568px)
and (device-width : 320px)
and (-webkit-min-device-pixel-ratio: 2)
/* and (orientation : todo: you can add orientation or delete this comment)*/ {
/*IPhone 5 only CSS here*/
}
请注意,这里检查的是确切的宽度和高度,而不是最大宽度。
那么,解决方案是什么呢?如果您想编写一个在所有可能的设备上都能看起来很好的网页,最佳实践是使用退化模式。
/* 退化模式 我们只检查屏幕宽度 当从纵向切换到横向时,这肯定会改变 */
/*css for desktops here*/
@media (max-device-width: 1024px) {
/*IPad portrait AND netbooks, AND anything with smaller screen*/
/*make the design flexible if possible */
/*Structure your code thinking about all devices that go below*/
}
@media (max-device-width: 640px) {
/*Iphone portrait and smaller*/
}
@media (max-device-width: 540px) {
/*Smaller and smaller...*/
}
@media (max-device-width: 320px) {
/*IPhone portrait and smaller. You can probably stop on 320px*/
}
如果您网站的访问者中有超过30%来自移动设备,请颠倒此方案,采用以移动设备为先的方法。在这种情况下,请使用min-device-width
。这将加速移动浏览器的网页渲染。
对我来说,完成这项工作的查询是:
only screen and (device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2)
iPhone 5 在竖屏和横屏下的显示效果
@media only screen
and (min-device-width : 320px)
and (max-device-width : 568px) {
/* styles*/
}
iPhone 5 横屏显示
@media only screen
and (min-device-width : 320px)
and (max-device-width : 568px)
and (orientation : landscape) {
/* styles*/
}
竖屏下的iPhone 5
@media only screen
and (min-device-width : 320px)
and (max-device-width : 568px)
and (orientation : portrait) {
/* styles*/
}
max-device-width: 568px
? - adi518device-width: 320px
和 device-height: 568px
呢? - adi518对于针对PhoneGap应用的响应,以上方案均不适用。
如下链接所示,以下解决方案可行。
@media screen and (device-height: 568px) and (orientation: portrait) and (-webkit-min-device-pixel-ratio: 2) {
// css here
}
作为一个快速的补充,我测试了一些选项,在此过程中可能会忽略这个问题。确保你的页面包含:
<meta name="viewport" content="initial-scale=1.0">
http://pieroxy.net/blog/2012/10/18/media_features_of_the_most_common_devices.html
你甚至可以在同一网站的测试页面上获取自己设备的值。/* iPad */
@media screen and (min-device-width: 768px) {
/* ipad-portrait */
@media screen and (max-width: 896px) {
.logo{
display: none !important;
}
}
/* ipad-landscape */
@media screen and (min-width: 897px) {
}
}
/* iPhone */
@media screen and (max-device-width: 480px) {
/* iphone-portrait */
@media screen and (max-width: 400px) {
}
/* iphone-landscape */
@media screen and (min-width: 401px) {
}
}
据我所知,没有任何一款iPhone使用1.5的像素比率
iPhone 3G / 3GS: (-webkit-device-pixel-ratio: 1) iPhone 4G / 4GS / 5G: (-webkit-device-pixel-ratio: 2)