使用JavaScript可以检测设备的方向模式:
if (window.innerHeight > window.innerWidth) {
portrait = true;
} else {
portrait = false;
}
然而,有没有仅使用CSS来检测方向的方法?
例如:
@media only screen and (width > height) { ... }
使用JavaScript可以检测设备的方向模式:
if (window.innerHeight > window.innerWidth) {
portrait = true;
} else {
portrait = false;
}
然而,有没有仅使用CSS来检测方向的方法?
例如:
@media only screen and (width > height) { ... }
用CSS检测屏幕方向:
@media screen and (orientation:portrait) { … }
@media screen and (orientation:landscape) { … }
CSS中媒体查询的定义可以在http://www.w3.org/TR/css3-mediaqueries/#orientation找到。
@media all and (orientation:portrait) {
/* Style adjustments for portrait mode goes here */
}
@media all and (orientation:landscape) {
/* Style adjustments for landscape mode goes here */
}
但看起来你仍需要进行实验
我认为我们需要编写更具体的媒体查询。确保如果您编写一个媒体查询,它不会影响其他视图(移动,平板电脑,台式电脑),否则可能会出现问题。我建议为每个设备编写一个基本的媒体查询,它可以覆盖两个视图,并编写一个方向媒体查询,以便您可以更多地编写有关方向视图的特定代码,这是很好的实践。我们不需要同时编写两个媒体方向查询。您可以参考下面的示例。如果我的英语写作不太好,我很抱歉。
针对移动设备:
@media screen and (max-width:767px) {
..This is basic media query for respective device.In to this media query CSS code cover the both view landscape and portrait view.
}
@media screen and (min-width:320px) and (max-width:767px) and (orientation:landscape) {
..This orientation media query. In to this orientation media query you can specify more about CSS code for landscape view.
}
平板电脑适用
@media screen and (max-width:1024px){
..This is basic media query for respective device.In to this media query CSS code cover the both view landscape and portrait view.
}
@media screen and (min-width:768px) and (max-width:1024px) and (orientation:landscape){
..This orientation media query. In to this orientation media query you can specify more about CSS code for landscape view.
}
桌面
根据您的设计需求制作并享受...(:
谢谢, Jitu
我会选择aspect-ratio,它提供了更多的可能性。
/* Exact aspect ratio */
@media (aspect-ratio: 2/1) {
...
}
/* Minimum aspect ratio */
@media (min-aspect-ratio: 16/9) {
...
}
/* Maximum aspect ratio */
@media (max-aspect-ratio: 8/5) {
...
}
屏幕方向和宽高比都取决于视口的实际大小,与设备方向本身无关。
阅读更多:https://dev.to/ananyaneogi/useful-css-media-query-features-o7f
方向似乎不稳定。
使用宽高比可能是更好的解决方法。
@media only screen and (max-aspect-ratio: 1/1){
//portrait
}
@media only screen and (min-aspect-ratio: 1/1){
//horizontal
}
screen.width
和screen.height
。这两个值在所有现代浏览器中都可用。它们提供了屏幕的实际尺寸,即使在应用启动时浏览器被缩小。 window.innerWidth
在浏览器被缩小时会发生变化,在移动设备上不会发生,但在PC和笔记本电脑上可能会发生。
screen.width
和screen.height
的值在移动设备在纵向和横向模式之间切换时会发生变化,因此通过比较这些值可以确定模式。如果screen.width
大于1280px,则处理的是PC或笔记本电脑。screen.width
值的集中在320px(主要是iPhone),360px(大多数其他手机),768px(小型平板电脑)和800px(常规平板电脑)。screen.width
和 screen.height
的值。它始终报告相同的值。您必须使用 window.orientation
属性来确定设备是否已旋转...(在横向模式下,该值将为 90 或 -90)。 - interDist我们可以使用以下代码进行测试
@media all and (orientation:portrait) {
// simple css for portrait mode of mobile device
}
@media all and (orientation:landscape) {
// css for landscape mode
}
screen
而不是all
。 - maf88