【问题标题】:Bootstrap4 adding scrollbar to divBootstrap4 将滚动条添加到 div
【发布时间】:2017-02-27 23:39:20
【问题描述】:

我在div 中使用stacked pills。问题是,有时div 包含很多药片。如何为其添加滚动条?

<div class="row col-md-2">
<ul class="nav nav-pills nav-stacked">
  <li class="nav-item">
    <a class="nav-link active" href="#">Active</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Link</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#">Link</a>
  </li>
  <li class="nav-item">
    <a class="nav-link disabled" href="#">Disabled</a>
  </li>
</ul>
</div>

我可以为 div 添加一个类以使堆叠的药丸可滚动吗?

【问题讨论】:

    标签: bootstrap-4


    【解决方案1】:

    是的,有可能, 只需添加一个像anyclass 这样的类 并给出一些 CSS 样式。 Live

    .anyClass {
      height:150px;
      overflow-y: scroll;
    }
    

    .anyClass {
      height:150px;
      overflow-y: scroll;
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" rel="stylesheet"/>
    
    <div class=" col-md-2">
      <ul class="nav nav-pills nav-stacked anyClass">
        <li class="nav-item">
          <a class="nav-link active" href="#">Active</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li><li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li><li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li><li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li><li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
        <li class="nav-item">
          <a class="nav-link disabled" href="#">Disabled</a>
        </li>
      </ul>
    </div>

    【讨论】:

    • 有没有办法让高度等于屏幕高度?
    • @MuhammadDanish 是的,你可以使用height: 100vh,或者如果你有顶部栏或菜单,那么也可以使用height: calc(100vh - 100px) 注意:100px 是示例
    【解决方案2】:

    在包含元素的元素上使用overflow-y: scroll 属性。

    overflow-y 属性指定当块级元素在顶部和底部边缘溢出时,是否剪切内容、添加滚动条或显示溢出内容。

    有时在 overflow-y 属性旁边放置元素的高度很有趣,如下例所示:

    <ul class="nav nav-pills nav-stacked" style="height: 250px; overflow-y: scroll;">
      <li class="nav-item">
        <a class="nav-link active" href="#">Active</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
    

    【讨论】:

      【解决方案3】:
            <div class="overflow-auto p-3 mb-3 mb-md-0 mr-md-3 bg-light" style="max-width: 260px; max-height: 100px;">
              <strong>Column 0 </strong><br>
              <strong>Column 1</strong><br>
              <strong>Column 2</strong><br>
              <strong>Column 3</strong><br>
              <strong>Column 4</strong><br>
              <strong>Column 5</strong><br>
              <strong>Column 6</strong><br>
              <strong>Column 7</strong><br>
              <strong>Column 8</strong><br>
              <strong>Column 9</strong><br>
              <strong>Column 10</strong><br>
              <strong>Column 11</strong><br>
              <strong>Column 12</strong><br>
              <strong>Column 13</strong><br>
            </div>
          </div>
      

      【讨论】:

        【解决方案4】:

        .Scroll {
          height:600px;
          overflow-y: scroll;
        }
        <!DOCTYPE html>
        <html>
        <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script>
        </script>
        </head>
        <body>
        
        <h1>Smooth Scroll</h1>
        
        <div class="Scroll">
          <div class="main" id="section1">
            <h2>Section 1</h2>
            <p>Click on the link to see the "smooth" scrolling effect.</p>
            <p>Note: Remove the scroll-behavior property to remove smooth scrolling.</p>
          </div>
          <div class="main" id="section2">
            <h2>Section 2</h2>
            <p>Knowing how to write a paragraph is incredibly important. It’s a basic aspect of writing, and it is something that everyone should know how to do. There is a specific structure that you have to follow when you’re writing a paragraph. This structure helps make it easier for the reader to understand what is going on. Through writing good paragraphs, a person can communicate a lot better through their writing.</p>
          </div>
          <div class="main" id="section3">
            <h2>Section 3</h2>
            <p>Knowing how to write a paragraph is incredibly important. It’s a basic aspect of writing, and it is something that everyone should know how to do. There is a specific structure that you have to follow when you’re writing a paragraph. This structure helps make it easier for the reader to understand what is going on. Through writing good paragraphs, a person can communicate a lot better through their writing.</p>
          </div>
          <div class="main" id="section4">
            <h2>Section 4</h2>
            <p>Knowing how to write a paragraph is incredibly important. It’s a basic aspect of writing, and it is something that everyone should know how to do. There is a specific structure that you have to follow when you’re writing a paragraph. This structure helps make it easier for the reader to understand what is going on. Through writing good paragraphs, a person can communicate a lot better through their writing.</p>
          </div>
        
          <div class="main" id="section5">
            <h2>Section 5</h2>
            <a href="#section1">Click Me to Smooth Scroll to Section 1 Above</a>
          </div>
          <div class="main" id="section6">
            <h2>Section 6</h2>
            <p>Knowing how to write a paragraph is incredibly important. It’s a basic aspect of writing, and it is something that everyone should know how to do. There is a specific structure that you have to follow when you’re writing a paragraph. This structure helps make it easier for the reader to understand what is going on. Through writing good paragraphs, a person can communicate a lot better through their writing.</p>
          </div>
          <div class="main" id="section7">
            <h2>Section 7</h2>
            <a href="#section1">Click Me to Smooth Scroll to Section 1 Above</a>
          </div>
        </div>
        </body>
        </html>

        【讨论】:

        • 一般来说,如果答案包含对代码的用途的解释,以及为什么在不介绍其他人的情况下解决问题的原因,答案会更有帮助。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        • 1970-01-01
        相关资源
        最近更新 更多