如何使用 CSS 媒体查询检测设备方向?

发布于 2024-11-02 10:12:47 字数 297 浏览 1 评论 0原文

在 JavaScript 中,可以使用以下方式检测方向模式:

if (window.innerHeight > window.innerWidth) {
    portrait = true;
} else {
    portrait = false;
}

但是,有没有一种方法可以仅使用 CSS 来检测方向?

例如。像这样的东西:

@media only screen and (width > height) { ... }

In JavaScript the orientation mode can be detected using:

if (window.innerHeight > window.innerWidth) {
    portrait = true;
} else {
    portrait = false;
}

However, is there a way to detect the orientation using CSS only?

Eg. something like:

@media only screen and (width > height) { ... }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

你的心境我的脸 2024-11-09 10:12:47

用于检测屏幕方向的 CSS:

 @media screen and (orientation:portrait) { … }
 @media screen and (orientation:landscape) { … }

媒体查询的 CSS 定义位于 http://www .w3.org/TR/css3-mediaqueries/#orientation

CSS to detect screen orientation:

 @media screen and (orientation:portrait) { … }
 @media screen and (orientation:landscape) { … }

The CSS definition of a media query is at http://www.w3.org/TR/css3-mediaqueries/#orientation

长伴 2024-11-09 10:12:47
@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 all and (orientation:portrait) {
/* Style adjustments for portrait mode goes here */
}

@media all and (orientation:landscape) {
  /* Style adjustments for landscape mode goes here */
}

but it still looks like you have to experiment

凉城 2024-11-09 10:12:47

我认为我们需要编写更具体的媒体查询。确保如果您编写一个媒体查询,它不应该影响其他视图(Mob、Tab、Desk),否则可能会带来麻烦。我想建议为相应的设备编写一个基本媒体查询,其中涵盖视图和一个方向媒体查询,您可以针对方向视图具体编写更多代码,以获得良好的实践。我们不需要同时编写两个媒体方向查询。你可以参考我下面的例子。如果我的英语写作不太好,我很抱歉。
例如:

适用于移动设备

@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.

}

桌面

根据您的设计要求制作享受...(:

谢谢,
极土

I think we need to write more specific media query. Make sure if you write one media query it should be not effect to other view (Mob,Tab,Desk) otherwise it can be trouble. I would like suggest to write one basic media query for respective device which cover both view and one orientation media query that you can specific code more about orientation view its for good practice. we Don't need to write both media orientation query at same time. You can refer My below example. I am sorry if my English writing is not much good.
Ex:

For Mobile

@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.

}

For Tablet

@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.

}

Desktop

make as per your design requirement enjoy...(:

Thanks,
Jitu

眼泪也成诗 2024-11-09 10:12:47

我会选择纵横比,它提供了更多的可能性。

/* 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-媒体查询功能-o7f

I would go for aspect-ratio, it offers way more possibilities.

/* 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) {
    ...
}

Both, orientation and aspect-ratio depend on the actual size of the viewport and have nothing todo with the device orientation itself.

Read more: https://dev.to/ananyaneogi/useful-css-media-query-features-o7f

情释 2024-11-09 10:12:47

方向似乎没有按预期工作。
使用纵横比可能是更好的方法。

@media only screen and (max-aspect-ratio: 1/1) {
//portrait
}
@media only screen and (min-aspect-ratio: 1/1) {
//landscape
}

orientation does not seem to work as desired.
Working with aspect-ratio may be the better way.

@media only screen and (max-aspect-ratio: 1/1) {
//portrait
}
@media only screen and (min-aspect-ratio: 1/1) {
//landscape
}
伴随着你 2024-11-09 10:12:47

在 Javascript 中,最好使用 screen.width 和 screen.height。这两个值在所有现代浏览器中都可用。它们给出了屏幕的真实尺寸,即使应用程序启动时浏览器已缩小。当浏览器缩小时,window.innerWidth 会发生变化,这种情况在移动设备上不会发生,但在 PC 和笔记本电脑上可能会发生。

当移动设备在纵向和横向模式之间切换时,screen.widthscreen.height的值会发生变化,因此可以通过比较这些值来确定模式。如果 screen.width 大于 1280px,则您使用的是 PC 或笔记本电脑。

您可以在 Javascript 中构造一个事件侦听器来检测两个值何时翻转。需要关注的纵向 screen.width 值为 320px(主要是 iPhone)、360px(大多数其他手机)、768px(小型平板电脑)和 800px(普通平板电脑)。

In Javascript it is better to use screen.width and screen.height. These two values are available in all modern browsers. They give the real dimensions of the screen, even if the browser has been scaled down when the app fires up. window.innerWidth changes when the browser is scaled down, which can't happen on mobile devices but can happen on PCs and laptops.

The values of screen.width and screen.height change when the mobile device flips between portrait and landscape modes, so it is possible to determine the mode by comparing the values. If screen.width is greater than 1280px you're dealing with a PC or laptop.

You can construct an event listener in Javascript to detect when the two values are flipped. The portrait screen.width values to concentrate on are 320px (mainly iPhones), 360px (most other phones), 768px (small tablets) and 800px (regular tablets).

迷路的信 2024-11-09 10:12:47

我们可以简单地使用下面的代码进行测试

@media all and (orientation:portrait) {
 // simple css for portrait mode of mobile device
}

@media all and (orientation:landscape) {
 // css for landscape mode
}

更多信息: https://www.w3.org /TR/mediaqueries-3/#orientation

Simply we can test using below code

@media all and (orientation:portrait) {
 // simple css for portrait mode of mobile device
}

@media all and (orientation:landscape) {
 // css for landscape mode
}

More Info: https://www.w3.org/TR/mediaqueries-3/#orientation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文