如何使用 CSS 媒体查询检测设备方向?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
用于检测屏幕方向的 CSS:
媒体查询的 CSS 定义位于 http://www .w3.org/TR/css3-mediaqueries/#orientation
CSS to detect screen orientation:
The CSS definition of a media query is at http://www.w3.org/TR/css3-mediaqueries/#orientation
但看起来你仍然必须实验
but it still looks like you have to experiment
我认为我们需要编写更具体的媒体查询。确保如果您编写一个媒体查询,它不应该影响其他视图(Mob、Tab、Desk),否则可能会带来麻烦。我想建议为相应的设备编写一个基本媒体查询,其中涵盖视图和一个方向媒体查询,您可以针对方向视图具体编写更多代码,以获得良好的实践。我们不需要同时编写两个媒体方向查询。你可以参考我下面的例子。如果我的英语写作不太好,我很抱歉。
例如:
适用于移动设备
适用于平板电脑
桌面
根据您的设计要求制作享受...(:
谢谢,
极土
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
For Tablet
Desktop
make as per your design requirement enjoy...(:
Thanks,
Jitu
我会选择纵横比,它提供了更多的可能性。
方向和纵横比都取决于视口的实际大小,与设备方向本身无关。
了解更多:https://dev.to/ananyaneogi/useful-css-媒体查询功能-o7f
I would go for aspect-ratio, it offers way more possibilities.
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
方向
似乎没有按预期工作。使用纵横比可能是更好的方法。
orientation
does not seem to work as desired.Working with
aspect-ratio
may be the better way.在 Javascript 中,最好使用 screen.width 和 screen.height。这两个值在所有现代浏览器中都可用。它们给出了屏幕的真实尺寸,即使应用程序启动时浏览器已缩小。当浏览器缩小时,
window.innerWidth
会发生变化,这种情况在移动设备上不会发生,但在 PC 和笔记本电脑上可能会发生。当移动设备在纵向和横向模式之间切换时,
screen.width
和screen.height
的值会发生变化,因此可以通过比较这些值来确定模式。如果screen.width
大于 1280px,则您使用的是 PC 或笔记本电脑。您可以在 Javascript 中构造一个事件侦听器来检测两个值何时翻转。需要关注的纵向 screen.width 值为 320px(主要是 iPhone)、360px(大多数其他手机)、768px(小型平板电脑)和 800px(普通平板电脑)。
In Javascript it is better to use
screen.width
andscreen.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
andscreen.height
change when the mobile device flips between portrait and landscape modes, so it is possible to determine the mode by comparing the values. Ifscreen.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).
我们可以简单地使用下面的代码进行测试
更多信息: https://www.w3.org /TR/mediaqueries-3/#orientation
Simply we can test using below code
More Info: https://www.w3.org/TR/mediaqueries-3/#orientation