繁体   English   中英

在div中水平滚动

[英]Scroll horizontally inside a div

我试图在div中实现水平滚动。 它在较小的屏幕上可以正常工作。 对于大屏幕(桌面),div内容会追加到另一行。 如何避免这种情况?

HTML:

  <body>
<div class="container-fluid" id="scrollquestion" style="overflow-x:scroll;white-space: nowrap;overflow-y:hidden ;top: 500px; padding: 4px; width:auto;">

    <div class="col-sm-3 c">
        <h6>2</h6>

    </div>
    <div class="col-sm-3 c">
        <h6>3</h6>

    </div>
    <div class="col-sm-3 c">
        <h6>4</h6>

    </div>
    <div class="col-sm-3 c">
        <h6>5</h6>

    </div>
      <div class="col-sm-3 c">
        <h6>5</h6>

    </div>
      <div class="col-sm-3 c">
        <h6>5</h6>

    </div>
      <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
    <div class="col-sm-3 c">
        <h6> 5</h6>

    </div><div class="col-sm-3 c">
        <h6> 5</h6>

    </div><div class="col-sm-3 c">
        <h6> 5</h6>

    </div><div class="col-sm-3 c">
        <h6> 5</h6>

    </div><div class="col-sm-3 c">
        <h6> 5</h6>

    </div><div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
    <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
    <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
    <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
    <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
            <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
    <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
    <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
    <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
            <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
    <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
    <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
    <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
    <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
    <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
    <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
    <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
    <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>
    <div class="col-sm-3 c">
        <h6> 5</h6>

    </div>

</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/jquery.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>

CSS:

.col-sm-3{display: inline-block;width:auto;}
.c {
   color: #64cbcd;
   font-size: 11px;
   border-radius: 50%;
   width: 40px;
   display: inline-block;
   text-align: center;
   vertical-align: middle;
   border-color: blue;
   box-shadow: 0 0 0 3px #002525; // JUST ADD THIS LINE AND MODIFY YOUR COLOR
    padding-right: 10px;
    padding-left: 3px;
    margin-left: 10px;
    margin-right:10px
}

对于较小的屏幕,它可以按预期工作。 检查: https : //jsfiddle.net/rejithrkrishnan/jeoh5220/2/

在桌面中,如下图所示。 桌面图片

首先也是最重要的一点,如果您不想使用引导属性的.col-sm-3类,而现在却想回答问题“ .col-sm-3”的“ float:left”属性,为什么?搞砸了。 只需覆盖它,问题就会得到解决。

.c {
   float:none;
}

或者从HTML删除col-sm-3

没错:)

给出white-space: nowrap .c white-space: nowrap

.c {
  white-space: nowrap
}

我增加了容器的高度,对我有用!

它不可能通过CSS,使用jQuery,找到孩子的宽度并应用它

var width = 0;
$('.c').each(function() {
    width += $(this).outerWidth( true );
});
$('.horizontal-scroll').css('width', width+100);

DEMO

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM