jquery 如何在光滑转盘中添加数字计数器

wmtdaxz3  于 2023-08-04  发布在  jQuery
关注(0)|答案(1)|浏览(90)

现在我有数字01在左边持有类left__count05在右边持有类right__count,我想更新的数字,一旦我点击任何导航图标。例如,如果我单击第二个图标,数字将更新为0205等。
除此之外,如果我添加另一个导航图标/内容,05将动态更新到06,无论光滑旋转木马中的项目如何。
下面是我的代码,为了更好地理解,这里是沙盒代码https://codesandbox.io/

更新:。代码已更新,工作正常。然而,其他JS解决方案/替代方案是受欢迎的,而不使用.slick-track: left: 0 !important;的CSS。

$('.slider-for').slick({
   slidesToShow: 1,
   slidesToScroll: 1,
   arrows: false,
   infinite: false,
   draggable: false,
   swipe: false,
   swipeToSlide: false,
   touchMove: false,
   asNavFor: '.slider-nav'
 });
 $('.slider-nav').slick({
   slidesToShow: 5,
   slidesToScroll: 1,
   asNavFor: '.slider-for',
   dots: false,
   arrows: false,
   infinite: false,
   focusOnSelect: true,
   variableWidth: true
 });

function barAndCounter(index = 0) {
  //bar
  var progressBar = $('.progress-bar__inner');
  var itemWidth = 100 / $('.slider-nav .icons').length;
  var progress = (index + 1) * itemWidth;
  progressBar.css('width', progress + '%');
  //counter
  var sliderNav = $('.slider-nav');
  var totalSlides = sliderNav.slick('getSlick').slideCount;
  $(".left__count").text('0' + ++index);
  $(".right__count").text('0' + totalSlides);
}
// Will be triggered on page load
barAndCounter();
// Will be triggered on slider click
$('.slider-nav .icons').on('click', function() {
  var index = $(this).index();
  barAndCounter(index);
});
body{
  background:#ccc;
}
.container {
  font-family:Arial;
  max-width:800px;
  display:block;
  margin:0 auto;
  position: relative;
}
.slider-content {
  background: #fff;
  padding: 3rem;
  font-size: 3rem;
  text-align: center;
  height: 200px !important;
}
.slider-nav .slick-list .slick-current {
  background: green;
  color: white;
}
.slider-nav-wrapper {
  position: absolute;
  left:3rem;
  right: 3rem;
  bottom: 3rem;
}
.slider-nav-wrapper .icons {
  background: black;
  color: #3498db;
  font-size: 1rem;
  margin: 0 .5rem;
  position: relative;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 50px;
  height: 50px;
}
.progress-bar {
  width: 100%;
  height: 10px;
  border-radius: 10px;
  background: gray;
  position: relative;
  overflow: hidden;
}
.progress-bar__inner {
  background: green;
  position: absolute;
  height: 10px;
  top: 0;
  left: 0;
  width: 0;
  transition: width 0.3s ease;
}
.left__count {
  padding-right: 1rem;
}
.right__count {
  padding-left: 1rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script type="text/javascript" src="//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.css"/>

<div class="pagingInfo"></div>
<div class="container">
  <div class="slider slider-for">
    <div class="slider-content">1</div>
    <div class="slider-content">2</div>
    <div class="slider-content">3</div>
    <div class="slider-content">4</div>
    <div class="slider-content">5</div>
  </div>
  <div class="slider-nav-wrapper">
   <div class="slider slider-nav">
      <div class="icons">1</div>
      <div class="icons">2</div>
      <div class="icons">3</div>
      <div class="icons">4</div>
      <div class="icons">5</div>
    </div>
    <div style="display: flex; align-items: center; margin-top: 1rem;">
      <div class="left__count">01</div>
      <div class="progress-bar">
        <div class="progress-bar__inner"></div>
      </div> 
      <div class="right__count">05</div>
    </div>
  </div>
</div>
prdp8dxp

prdp8dxp1#

你可以很容易地得到左边的柜台变化。只需将此添加到触发progressBar函数的函数中:

// Will be triggered on slider click
$('.slider-nav .icons').on('click', function() {
  var index = $(this).index();
  $(".left__count").text(++index)
  progressBar(index);
});

字符串
关于你的右边柜台你到底需要什么?你能说得更具体点吗

相关问题