我試圖在水平滾動div
中顯示圖像列表(等高)。這很有效,但是當我想要有一個固定的圖像時 - 一個「封面」圖像出現在容器內部最左側,佈局會被搞砸。如何在固定項目旁邊顯示水平滾動列表?
下面是我的工作的CSS和HTML。 如果您運行該代碼段,您可以看到列表跳轉到下一行,而不是保持與「封面」圖像相鄰並且水平滾動。這裏是的jsfiddle - http://jsfiddle.net/6x66dLdy/
我可以使用JavaScript通過設置#list
寬度編程方式解決這個問題,但我想單獨如果可能的話CSS來做到這一點。
#container {
height: 120px;
background: #ccccff;
}
#cover {
height: 100px;
margin: 10px;
display: inline-block;
vertical-align: top;
position: relative;
}
#cover img {
border: 2px solid #cc0000;
}
#list {
overflow-x: scroll;
overflow-y: hidden;
white-space: nowrap;
height: 100px;
margin: 10px 0;
display: inline-block;
}
.item {
height: 80px;
margin: 10px 5px;
display: inline-block;
}
<div id="container">
<div id="cover">
<img src="http://placehold.it/160x100"/>
</div>
<div id="list">
<div class="item">
<img src="http://placehold.it/120x80"/>
</div>
<div class="item">
<img src="http://placehold.it/60x80"/>
</div>
<div class="item">
<img src="http://placehold.it/120x80"/>
</div>
<div class="item">
<img src="http://placehold.it/120x80"/>
</div>
<div class="item">
<img src="http://placehold.it/120x80"/>
</div>
</div>
</div>
爲什麼在css中沒有#list {width:100px;}? – ashkufaraz 2014-11-21 12:16:17
你必須爲你的'div'和容器提供寬度。像這樣:http://jsfiddle.net/abhitalks/6x66dLdy/1/ – Abhitalks 2014-11-21 12:18:09
@abhitalks:謝謝!請加上'calc'作爲你的答案。這是我大致期待的。 – 2014-11-21 12:33:16