我正在使用AngularJS和SCSS編碼網站。我處於開發的移動階段,我很快發現(對於這個項目),我需要一種使用@media
查詢來定位多個斷點的方法。所以我通過this SO answer和this CSS Tricks Post找到了SO以及其他多個答案。然後我在測試用例中實現了我找到的解決方案,請參閱下面的測試代碼片段。逗號分隔的媒體查詢列表不起作用
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是真的,那麼做一些事情」。如果方向是橫向或縱向,查詢將是真實的,爲什麼它甚至是一個條件? – Godwin
我的想法是,我想根據設備的方向來定位特定元素。當我忽略方向時,我說「這些風格可以適用於肖像或風景,無所謂。」但是,如前所述,我只想在Landscape上顯示某些東西,但已經隱藏在Portrait上。 – mrClean
我的意思是,據我所知,你的陳述等同於'A || (A && B)'可以簡化爲'A',在我看來,你的邏輯中有一個錯誤。就目前而言,無論風景如何,每當寬度大於大斷點時,都會應用「淺綠色」背景色。 – Godwin