【发布时间】:2021-08-14 04:20:03
【问题描述】:
两个按钮左右滚动 DIV。但是,我有这样 5 个这样的滚动条,并且 Javascript 变得重复。我们如何优化它?
PS:演示版仅显示 2 个滚动条。
DEMO JSFiddle:https://jsfiddle.net/8Lxw1bha/
HTML
<div class="nav-scroller">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="row nav" id="boxSliderFirst">
<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>
<input id="btnLeftFirst" type="Button" value="Prev"/>
<input id="btnRightFirst" type="Button" value="Next" />
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="row nav" id="boxSliderSecond">
<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>
<input id="btnLeftSecond" type="Button" value="Prev"/>
<input id="btnRightSecond" type="Button" value="Next" />
</div>
</div>
</div>
</div>
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:
/* first scroller */
const boxSliderFirst = document.getElementById('boxSliderFirst');
document.getElementById("btnLeftFirst").onclick = () => {
boxSliderFirst.scroll({
left: boxSliderFirst.scrollLeft + boxSliderFirst.querySelector(".col-md-3").offsetWidth,
behavior: 'smooth'
});
}
document.getElementById("btnRightFirst").onclick = () => {
boxSliderFirst.scroll({
left: boxSliderFirst.scrollLeft - boxSliderFirst.querySelector(".col-md-3").offsetWidth,
behavior: 'smooth'
});
}
/* second scroller */
const boxSliderSecond = document.getElementById('boxSliderSecond');
document.getElementById("btnLeftSecond").onclick = () => {
boxSliderSecond.scroll({
left: boxSliderSecond.scrollLeft + boxSliderSecond.querySelector(".col-md-3").offsetWidth,
behavior: 'smooth'
});
}
document.getElementById("btnRightSecond").onclick = () => {
boxSliderSecond.scroll({
left: boxSliderSecond.scrollLeft - boxSliderSecond.querySelector(".col-md-3").offsetWidth,
behavior: 'smooth'
});
}
【问题讨论】:
-
看起来一切都相同,除了您选择的两个 HTML
ids。您可以将逻辑提取到一个接受两个参数(每个参数分别为id)的函数中。然后为所有用例调用该函数 -
是的,有 5 个这样的 ID。我不确定如何为所有情况调用该函数。
标签: javascript jquery css twitter-bootstrap