Bootstrap 水平滚动

pgpifvop  于 2023-10-14  发布在  Bootstrap
关注(0)|答案(3)|浏览(157)

我使用以下HTML代码:

<div id="list">
  <div class="row">
    <div class="col-md-3">1</div>
    <div class="col-md-3">2</div>
    <div class="col-md-3">3</div>
    <div class="col-md-3">n-times</div> 
  </div>
</div>

我试图显示一行,其中包含无限多列,当它们溢出容器时会滚动。
我该怎么做?
我尝试为#list容器设置一个固定的width,并且还设置了overflow-x: auto

nhhxz33t

nhhxz33t1#

okay to exceed 12 column units in a row。它会导致列换行,但您可以使用flexbox覆盖换行。

Bootstrap 4使用flexbox和实用程序类来获得水平滚动布局。

<div class="container-fluid">
    <div class="row flex-row flex-nowrap">
        <div class="col-3">
           ..
        </div>
        <div class="col-3">
           ..
        </div>
        <div class="col-3">
           ..
        </div>
        <div class="col-3">
           ..
        </div>
        <div class="col-3">
           ..
        </div>
        <div class="col-3">
           ..
        </div>
        <div class="col-3">
           ..
        </div>
        <div class="col-3">
           ..
        </div>
    </div>
</div>

Bootstrap 4 Demo:http://codeply.com/go/GoFQqQAFhN

另请参阅:Bootstrap中可水平滚动的牌列表
对于Bootstrap 3,它将与一些CSS的flexbox...

row > .col-xs-3 {
    display:flex;
    flex: 0 0 25%;
    max-width: 25%
}

.flex-nowrap {
    -webkit-flex-wrap: nowrap!important;
    -ms-flex-wrap: nowrap!important;
    flex-wrap: nowrap!important;
}
.flex-row {
    display:flex;
    -webkit-box-orient: horizontal!important;
    -webkit-box-direction: normal!important;
    -webkit-flex-direction: row!important;
    -ms-flex-direction: row!important;
    flex-direction: row!important;
}

Bootstrap 3 Demohttp://codeply.com/go/P13J3LuBIM

sdnqo3pr

sdnqo3pr2#

另一种方法:

CSS

#list .row {white-space:nowrap;}
#list .row > div {display:inline-block;float:none;}

横向滚动Js:

window.addEventListener('mousewheel', function(e){
    e.preventDefault();
    var step = -100;  
    if (e.wheelDelta < 0) {
      step *= -1;
    }
    var newPos = window.pageXOffset + step;
    $('body').scrollLeft(newPos);    
})

Bootplyhttps://www.bootply.com/pbenard/usmX12rxgP

uubf1zoe

uubf1zoe3#

Carol解决方案的bootstrap 5变体是

<div class="row overflow-x-auto flex-nowrap">
  <!-- your cols here -->
</div>

相关问题