html - 带有 flexbox 的旋转木马 slider ,防止水平滚动

标签 html css flexbox

我正在尝试使用 flexbox 制作旋转木马 slider 。 问题是我只需要显示 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>

最佳答案

这可以通过稍微修改您的代码来完成。我已经将 flex-grow 属性添加到 flex 行中的三个容器以及 overflow: scrollwhite-space: nowrap 到你的轮播并设置你的 .item to 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;
}
<div class="main-container">
  <div class="prev">&laquo;</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">&raquo;</div>
</div>

关于html - 带有 flexbox 的旋转木马 slider ,防止水平滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46117549/

相关文章:

html - 如何在没有@media 查询的情况下创建响应式 div 宽度

html - 为什么我的下拉菜单没有在我的页面上完全展开?

ruby - 从标签中提取 HTML5 数据属性

html - CSS:是什么导致了这种奇怪的排列?

html - 为什么 svg 和 css 屏蔽(clippathing)不起作用

html - 等高列并将最后一项与底部对齐

html - 使用 CSS3 媒体查询时减少 HTTP 请求

jquery - 鼠标悬停动画不适用于鼠标快速移动

html - 使flexbox元素居中和右对齐

html - 使用flexbox,图像溢出整个容器