我试图使用flexbox制作旋转木马滑块。
问题是我只需显示4个项目并阻止滚动,而.carousel
占据全宽并显示滚动的所有项目。
是否可以使用flexbox执行此操作?
.main - container {
display: flex;
flex - direction: row;
overflow: hidden;
min - width: 0;
}
.prev,
.next {
flex: initial;
width: 50 px;
}
.carousel {
flex: 1;
}
<div class="main-container">
<div class="prev"></div>
<div class="carousel">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="nex"></div>
</div>
答案 0 :(得分:0)
这可以通过对代码稍作修改来完成。我已将弹性增长属性添加到弹性行中的三个容器,并将overflow: scroll
和white-space: nowrap
添加到您的轮播,并将.item
设置为display: inline-block
;
.main-container {
display: flex;
flex-direction: row;
min-width: 0;
}
.prev,
.next {
width: 50 px;
flex-grow: 1;
background-color: #fcfcfc;
}
.carousel {
flex-grow: 2;
background-color: #fbfbfb;
overflow: scroll;
white-space: nowrap;
}
.item {
display: inline-block;
}
&#13;
<div class="main-container">
<div class="prev">«</div>
<div class="carousel">
<div class="item"><img src="https://www.fillmurray.com/300/300"/></div>
<div class="item"><img src="https://www.fillmurray.com/300/300"/></div>
<div class="item"><img src="https://www.fillmurray.com/300/300"/></div>
<div class="item"><img src="https://www.fillmurray.com/300/300"/></div>
<div class="item"><img src="https://www.fillmurray.com/300/300"/></div>
<div class="item"><img src="https://www.fillmurray.com/300/300"/></div>
<div class="item"><img src="https://www.fillmurray.com/300/300"/></div>
</div>
<div class="nex">»</div>
</div>
&#13;