【问题标题】:Scroll column-wise horizontally on clicking Prev/Next in bootstrap 5在引导程序 5 中单击 Prev/Next 时水平滚动列
【发布时间】:2021-05-25 21:32:45
【问题描述】:

我创建了 2 个左右滚动 DIV 的按钮。但是,它滚动一个固定的宽度。我希望它在单击 Prev/Next 时滚动 Bootstrap 5 的每一列。

或者更简单地说,我希望它根据点击向左或向右滚动一列,而不是像当前那样每列滚动 100 像素。请帮忙。

DEMO JSFiddle:https://jsfiddle.net/hsmx2f5z/

HTML

<div class="nav-scroller">
  <div class="container">
    <div class="row">
      <div class="col-md-12">
        <div class="row nav" id="boxSlider">
          <div class="col-md-3 nav-scroller__eighty">
            <div class="card">
              <div class="card-body">
                <h5><a href="#">Title 1</a></h5>
                <p>Some information about the slider. Some information about the slider. Some information about the slider. Some information about the slider.</p>
              </div>
            </div>
          </div>
          <div class="col-md-3 nav-scroller__eighty">
            <div class="card">
              <div class="card-body">
                <h5><a href="#">Title 2</a></h5>
                <p>Some information about the slider. Some information about the slider. Some information about the slider. Some information about the slider.</p>
              </div>
            </div>
          </div>
          <div class="col-md-3 nav-scroller__eighty">
            <div class="card">
              <div class="card-body">
                <h5><a href="#">Title 3</a></h5>
                <p>Some information about the slider. Some information about the slider. Some information about the slider. Some information about the slider.</p>
              </div>
            </div>
          </div>
          <div class="col-md-3 nav-scroller__eighty">
            <div class="card">
              <div class="card-body">
                <h5><a href="#">Title 4</a></h5>
                <p>Some information about the slider. Some information about the slider. Some information about the slider. Some information about the slider.</p>
              </div>
            </div>
          </div>
          <div class="col-md-3 nav-scroller__eighty">
            <div class="card">
              <div class="card-body">
                <h5><a href="#">Title 5</a></h5>
                <p>Some information about the slider. Some information about the slider. Some information about the slider. Some information about the slider.</p>
              </div>
            </div>
          </div>
          <div class="col-md-3 nav-scroller__eighty">
            <div class="card">
              <div class="card-body">
                <h5><a href="#">Title 6</a></h5>
                <p>Some information about the slider. Some information about the slider. Some information about the slider. Some information about the slider.</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<input id="btnLeft" type="Button" value="Prev"/>
<input id="btnRight" type="Button" value="Next" />

CSS:

.nav-scroller {
  position: relative;
  z-index: 2;
  overflow-y: hidden;
}
.nav-scroller .nav {
  display: flex;
  flex-wrap: nowrap;
  margin-top: -1px;
  overflow-x: auto;
  color: rgba(255, 255, 255, .75);
  text-align: center;
  white-space: nowrap;
  -webkit-overflow-scrolling: touch;
}

.nav-underline .nav-link {
  padding-top: 50px;
  padding-bottom: 50px;
  font-size: 14px;
  color: #6c757d;
  min-width: 100px;
}

.nav-underline .nav-link:hover {
  color: #007bff;
}
.card-body p {
  color: #393939;
  white-space: pre-wrap;
}
.nav-underline .active {
  font-weight: 500;
  color: #343a40;
}
.nav-scroller__eighty {
  width: 80%;
  max-width: 80%;
}

JS:

const boxSlider = document.getElementById('boxSlider');

document.getElementById("btnLeft").onclick = () => {
  boxSlider.scroll({
         left: boxSlider.scrollLeft + 200,
         behavior: 'smooth'
  });
}
     
document.getElementById("btnRight").onclick = () => {
  boxSlider.scroll({
         left: boxSlider.scrollLeft - 200,
         behavior: 'smooth'
  });
}

【问题讨论】:

    标签: javascript jquery css bootstrap-4 bootstrap-5


    【解决方案1】:

    您最好的选择可能是根据列元素的宽度滚动,如下所示:

      boxSlider.scroll({
        left: boxSlider.scrollLeft + widthOfColumn,
        behavior: 'smooth'
      });
    

    你可以像这样得到一列的宽度:

    // Assuming all columns are the same width, get the width of the first column
    boxSlider.querySelector(".nav-scroller__eighty").offsetWidth
    

    你的代码应该是这样的

    const boxSlider = document.getElementById('boxSlider');
    
    document.getElementById("btnLeft").onclick = () => {
      boxSlider.scroll({
        left: boxSlider.scrollLeft + boxSlider.querySelector(".nav-scroller__eighty").offsetWidth,
        behavior: 'smooth'
      });
    }
         
    document.getElementById("btnRight").onclick = () => {
      boxSlider.scroll({
        left: boxSlider.scrollLeft - boxSlider.querySelector(".nav-scroller__eighty").offsetWidth,
        behavior: 'smooth'
      });
    }
    

    【讨论】:

    • 这可行,但我忘了提到有 5 种不同的此类滚动条,我相信我们不能使用超过 1 个 id="boxSlider"。有什么解决办法吗?
    • @ElaineByene 如果你有超过 1 个,只需调用下一个 botSlider2,或者将它们标记在一个类下并循环执行相同的操作。
    • 有了这个答案,您可以随意设置、调整和清理代码和 HTML。只要您保持 Javascript 完好无损,它就可以在 Bootstrap 5 中完美运行。事实上:省略 prev/next 按钮使它成为一个非常棒的移动滚动解决方案,在使用 (Bootstrap) CSS 对其进行样式设置时使用。您甚至可以将 'nav-scroller__eighty' 设置为 Bootstrap 按钮。
    猜你喜欢
    • 2023-03-20
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 2017-06-17
    • 2017-02-13
    • 2018-11-03
    相关资源
    最近更新 更多