【发布时间】:2016-11-15 03:15:21
【问题描述】:
我正在用 AngularJS 和 SCSS 编写一个网站。我正处于开发的移动阶段,我很快发现(对于这个项目)我需要一种方法来使用@media 查询来定位多个断点。所以我通过this SO answer 和this CSS Tricks Post 以及关于SO 的多个其他答案找到了。然后我将找到的解决方案实现到一个测试用例中,测试见下面的 sn-p。
main {
background-color: grey;
height: 100%;
min-height: 1000px;
@media (max-width: 992px) {
background-color: red
}
@media (max-width: 768px) {
background-color: lightcoral
}
@media (max-width: 992px), (max-width: 992px) and (orientation: landscape) {
background-color: lightgreen;
}
@media (max-width: 768px),
(max-width: 768px) and (orientation: landscape) {
background-color: lightblue;
// Reset the min-height here, because we no longer have the sticky search bar.
min-height: 450px;
}
}
<main>
<h1>Page Title</h1>
<h2>Some Descriptive information</h2>
<div>Content</div>
</main>
但我无法让它工作。最终,我想要做的是当用户在平板电脑或手机上处于横向时应用样式。但是,我不知道我做对了,还是正确使用了or 运算符。
这显然不起作用,好吧,第一个语句(例如:(max-width: 992px))有效,但第二个语句的计算结果不正确。根据 Mozilla:
逗号分隔列表的行为类似于逻辑运算符或在媒体查询中使用时。使用逗号分隔的媒体查询列表时,如果任何媒体查询返回 true,则应用样式或样式表。逗号分隔列表中的每个媒体查询都被视为一个单独的查询,应用于一个媒体查询的任何运算符都不会影响其他查询。 --- Mozilla Documentation
即使我将代码分成两个单独的媒体查询:
@media (max-width: 992px) {
background-color: lightgreen;
}
@media (max-width: 992px) and (orientation: landscape) {
background-color: lightgreen;
}
还是不行。所以我不知道我的目标是错误的宽度(横向时)还是我做错了什么。有没有其他前端开发人员可以告诉我为什么我的逗号分隔媒体查询不起作用?
编辑:这是原生 SCSS 代码:
main {
background-color: $mono-90;
height: 100%;
min-height: 1000px;
@media screen and (max-width: map_get($grid-breakpoints, 'md')) {
// Reset the min-height here, because we no longer have the sticky search bar.
min-height: 450px;
}
@media
(max-width: map_get($grid-breakpoints, 'lg')),
(max-width: map_get($grid-breakpoints, 'lg')) and (orientation: landscape){
background-color: lightgreen;
}
@media
(max-width: map_get($grid-breakpoints, 'md')),
(max-width: map_get($grid-breakpoints, 'md')) and (orientation: landscape){
background-color: lightblue;
}
@media
(max-width: map_get($grid-breakpoints, 'sm')),
(max-width: map_get($grid-breakpoints, 'sm')) and (orientation: landscape){
background-color: lightcoral;
}
}
编辑:根据@Godwin 的建议,我将@media 查询简化为:
main {
background-color: $mono-90;
height: 100%;
min-height: 1000px;
@media screen and (max-width: map_get($grid-breakpoints, 'md')) {
// Reset the min-height here, because we no longer have the sticky search bar.
min-height: 450px;
}
@media screen and (max-width: map_get($grid-breakpoints, 'lg')) {
background-color: lightgreen;
}
@media screen and (max-width: map_get($grid-breakpoints, 'md')) {
background-color: lightblue;
}
@media screen and (max-width: map_get($grid-breakpoints, 'sm')) {
background-color: lightcoral;
}
}
但是,它不适用于 iPad 横向 (1024x768)。我不希望它在笔记本电脑上显示,但确实希望它在 iPad 上横向显示。
【问题讨论】:
-
从逻辑上讲,这并没有什么意义。您本质上是在说“如果 A 为真,或者如果 A 和 B 为真,则做某事”。如果方向是横向或纵向,则查询将为真,那为什么它甚至是一个条件?
-
我的想法是我想根据设备的方向定位特定元素。当我省略方向时,我是在说“这些样式可以应用于纵向或横向,没关系。”但是,如前所述,有某些东西我只想在横向上显示,但在纵向上已经隐藏了。
-
我的意思是,据我了解,您的陈述等同于
A || (A && B),可以简化为A,在我看来,你的逻辑错误。就目前而言,只要宽度大于大断点,就会应用lightgreenbackground-color,无论景观如何。 -
我有这样的怀疑,非常好。我会试一试,看看它是否能解决问题。
-
我试过了,但还是有问题。我不知道我是否需要对大屏幕(逗号分隔)使用另一个媒体查询,但即使使用上面的代码,它在 iPad 环境中也不起作用。有什么想法吗?
标签: html css media-queries