英文:
How to add progress bar on slick carousel
问题
我有一个1-5的滑块导航图标,如果我点击其中任何一个,绿色的进度条会更新。
例如,如果我点击项目2,进度条将变为40%的绿色,不管slick轮播中有多少项目,重要的是绿色的背景颜色会更新。所以就像slickCount/100
或slickCount*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 "progress-bar__inner"
.
Updates:.
Code have been updated here https://codesandbox.io/.
HTML:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-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>
<!-- 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 -->
$('.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 + '%');
});
<!-- 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() {
$('.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 + '%');
}
// Will be triggered on page load
progressBar();
// Will be triggered on slider click
$('.slider-nav .icons').on('click', 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 -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论