1 回答

TA貢獻2012條經驗 獲得超12個贊
得益于出色的flexbox規范,這在CSS中是可行的。使用order和flex-flow屬性,我們可以實現您想要的。無前綴的IE11和所有常綠瀏覽器將支持此功能。IE10前綴-ms-order,不支持flex-flow。
該解決方案考慮了您列出的所有約束:
將指定順序的元素列表顯示為一行。
如果窗口太小,請更改它們以顯示在列中。
當元素在列中顯示時,更改它們的順序。
由于堆棧代碼段的限制,您需要以整頁模式查看演示,并調整瀏覽器的大小以查看效果。
.container div {
width: 100px;
height: 50px;
display: inline-block;
}
.one { background: red; }
.two { background: orange; }
.three { background: yellow; }
.four { background: green; }
.five { background: blue; }
@media screen and (max-width: 531px) {
.container { display: flex; flex-flow: column; }
.five { order: 1; }
.four { order: 2; }
.three { order: 3; }
.two { order: 4; }
.one { order: 5 }
}
<div class="container">
<div class="one">I'm first</div>
<div class="two">I'm second</div>
<div class="three">I'm third</div>
<div class="four">I'm fourth</div>
<div class="five">I'm fifth</div>
</div>
另外,這是一個JSFiddle演示。
如果您傾向于冗長的CSS,也可以在flex-flow: column-reverse不將order屬性分配給每個div的情況下直接使用。相同的演示限制適用;全屏查看此演示并相應地調整瀏覽器窗口的大小。
.container div {
width: 100px;
height: 50px;
display: inline-block;
}
.one { background: red; }
.two { background: orange; }
.three { background: yellow; }
.four { background: green; }
.five { background: blue; }
@media screen and (max-width: 531px) {
.container { display: flex; flex-flow: column-reverse; }
}
<div class="container">
<div class="one">I'm first</div>
<div class="two">I'm second</div>
<div class="three">I'm third</div>
<div class="four">I'm fourth</div>
<div class="five">I'm fifth</div>
</div>
值得指出的是,這flex-flow
是同時包含flex-direction
和flex-wrap
屬性的簡寫屬性。
- 1 回答
- 0 關注
- 563 瀏覽
相關問題推薦
添加回答
舉報