如何在slick轮播上添加进度条

huangapple go评论50阅读模式
英文:

How to add progress bar on slick carousel

问题

我有一个1-5的滑块导航图标,如果我点击其中任何一个,绿色的进度条会更新。

例如,如果我点击项目2,进度条将变为40%的绿色,不管slick轮播中有多少项目,重要的是绿色的背景颜色会更新。所以就像slickCount/100slickCount*100,但我不确定。我的进度条类是"progress-bar__inner"

更新:
代码已经在这里更新 https://codesandbox.io/

HTML:

<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 class="progress-bar">
      <div class="progress-bar__inner"></div>
    </div>   
  </div>
</div>

CSS:

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;
}

.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;
  margin-top: 1rem;
}

.progress-bar__inner {
  background: green;
  position: absolute;
  height: 10px;
  top: 0;
  left: 0;
  width: 0;
  transition: width 0.3s ease;
}

JS:

$('.slider-for').slick({
  slidesToShow: 1,
  slidesToScroll: 1,
  arrows: false,
  fade: 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,
  focusOnSelect: true,
  variableWidth: true
});

$('.slider-nav .icons').on('click', function() {
  var index = $(this).index();
  var progressBar = $('.progress-bar__inner');
  var itemWidth = 100 / $('.slider-nav .icons').length;
  var progress = (index + 1) * itemWidth;

  progressBar.css('width', progress + '%');
});
英文:

I have a slider nav icons of 1-5 and if I click any of it, progress bar of green will update.

For example If i click the item 2, the progress bar will be 40% of green etc...regardless of how many items are in the slick carousel the important thing is the green bgcolor will update. So its just like slickCount/100 or slickCount*100 but Im not sure about that. My progress bar class is &quot;progress-bar__inner&quot;.

Updates:.
Code have been updated here https://codesandbox.io/.

HTML:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;slider slider-for&quot;&gt;
    &lt;div class=&quot;slider-content&quot;&gt;1&lt;/div&gt;
    &lt;div class=&quot;slider-content&quot;&gt;2&lt;/div&gt;
    &lt;div class=&quot;slider-content&quot;&gt;3&lt;/div&gt;
    &lt;div class=&quot;slider-content&quot;&gt;4&lt;/div&gt;
    &lt;div class=&quot;slider-content&quot;&gt;5&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;slider-nav-wrapper&quot;&gt;
   &lt;div class=&quot;slider slider-nav&quot;&gt;
      &lt;div class=&quot;icons&quot;&gt;1&lt;/div&gt;
      &lt;div class=&quot;icons&quot;&gt;2&lt;/div&gt;
      &lt;div class=&quot;icons&quot;&gt;3&lt;/div&gt;
      &lt;div class=&quot;icons&quot;&gt;4&lt;/div&gt;
      &lt;div class=&quot;icons&quot;&gt;5&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;progress-bar&quot;&gt;
      &lt;div class=&quot;progress-bar__inner&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;   
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

CSS:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

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;
}
.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;
  margin-top: 1rem;
}
.progress-bar__inner {
  background: green;
  position: absolute;
  height: 10px;
  top: 0;
  left: 0;
  width: 0;
  transition: width 0.3s ease;
}

<!-- end snippet -->

JS:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

$(&#39;.slider-nav .icons&#39;).on(&#39;click&#39;, function() {
    var index = $(this).index();
    var progressBar = $(&#39;.progress-bar__inner&#39;);
    var itemWidth = 100 / $(&#39;.slider-nav .icons&#39;).length;
    var progress = (index + 1) * itemWidth;

    progressBar.css(&#39;width&#39;, progress + &#39;%&#39;);
  });

<!-- end snippet -->

答案1

得分: 1

文档中可以找到回调操作。

例如,我们可以使用 beforeChange 操作来实现这一点。
您可以稍后更改动画类型或回调方法。

$(document).ready(function() {
    $('.slider-for').slick({
        slidesToShow: 1,
        slidesToScroll: 1,
        arrows: false,
        fade: 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,
        focusOnSelect: true,
        variableWidth: true
    });

    function progressBar(index = 0) {
        var progressBar = $('.progress-bar__inner');
        var itemWidth = 100 / $('.slider-nav .icons').length;
        var progress = (index + 1) * itemWidth;

        progressBar.css('width', progress + '%');
    }

    // 页面加载时触发
    progressBar();

    // 单击滑块时触发
    $('.slider-nav .icons').on('click', function() {
        var index = $(this).index();
        progressBar(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;
}
.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;
  margin-top: 1rem;
}
.progress-bar__inner {
  background: green;
  position: absolute;
  height: 10px;
  top: 0;
  left: 0;
  width: 0;
  transition: width 0.3s ease;
}
<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="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 class="progress-bar">
      <div class="progress-bar__inner"></div>
    </div>   
  </div>
</div>
英文:

You can find in documentation callback actions.

For example we can use beforeChange action for this.
You can later change animation type or callback method.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {
    $(&#39;.slider-for&#39;).slick({
        slidesToShow: 1,
        slidesToScroll: 1,
        arrows: false,
        fade: false,
        draggable: false,
        swipe: false,
        swipeToSlide: false,
        touchMove: false,
        asNavFor: &#39;.slider-nav&#39;
    });
    $(&#39;.slider-nav&#39;).slick({
        slidesToShow: 5,
        slidesToScroll: 1,
        asNavFor: &#39;.slider-for&#39;,
        dots: false,
        arrows: false,
        focusOnSelect: true,
        variableWidth: true
    });

    function progressBar(index = 0) {
        var progressBar = $(&#39;.progress-bar__inner&#39;);
        var itemWidth = 100 / $(&#39;.slider-nav .icons&#39;).length;
        var progress = (index + 1) * itemWidth;

        progressBar.css(&#39;width&#39;, progress + &#39;%&#39;);
    }

    // Will be triggered on page load
    progressBar();

    // Will be triggered on slider click
    $(&#39;.slider-nav .icons&#39;).on(&#39;click&#39;, function() {
        var index = $(this).index();
        progressBar(index);
    });

});

<!-- language: lang-css -->

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;
}
.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;
  margin-top: 1rem;
}
.progress-bar__inner {
  background: green;
  position: absolute;
  height: 10px;
  top: 0;
  left: 0;
  width: 0;
  transition: width 0.3s ease;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot; src=&quot;//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.min.js&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.css&quot;/&gt;


&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;slider slider-for&quot;&gt;
    &lt;div class=&quot;slider-content&quot;&gt;1&lt;/div&gt;
    &lt;div class=&quot;slider-content&quot;&gt;2&lt;/div&gt;
    &lt;div class=&quot;slider-content&quot;&gt;3&lt;/div&gt;
    &lt;div class=&quot;slider-content&quot;&gt;4&lt;/div&gt;
    &lt;div class=&quot;slider-content&quot;&gt;5&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;slider-nav-wrapper&quot;&gt;
   &lt;div class=&quot;slider slider-nav&quot;&gt;
      &lt;div class=&quot;icons&quot;&gt;1&lt;/div&gt;
      &lt;div class=&quot;icons&quot;&gt;2&lt;/div&gt;
      &lt;div class=&quot;icons&quot;&gt;3&lt;/div&gt;
      &lt;div class=&quot;icons&quot;&gt;4&lt;/div&gt;
      &lt;div class=&quot;icons&quot;&gt;5&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;progress-bar&quot;&gt;
      &lt;div class=&quot;progress-bar__inner&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;   
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月20日 19:10:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定