Bootstrap horizontal scrolling

青春壹個敷衍的年華 提交于 2019-12-28 15:30:15

问题


I use the following HTML code:

<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>

So, I need to display one row with infinity columns by horizontal with scrolling in the bottom of page.

How can I do this?

So, I tried to set fixed width for list container and have set overflow-x: auto


回答1:


It's okay to exceed 12 column units in a row. It causes the columns to wrap, but you can override the wrapping with flexbox.

Bootstrap 4 includes flexbox, and the utility classes to get a horizontal scrolling layout..

<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://www.codeply.com/go/GoFQqQAFhN

For Bootstrap 3, it could be done with some CSS for the 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 Demo: http://www.codeply.com/go/P13J3LuBIM




回答2:


One other way :

CSS:

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

Js for horizontal scrolling:

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

Bootply : https://www.bootply.com/pbenard/usmX12rxgP



来源:https://stackoverflow.com/questions/41962898/bootstrap-horizontal-scrolling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!