未加星标

Building a Simple Progress Bar for Slick Slider

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Building a Simple Progress Bar for Slick Slider

This week I found myself needing to develop a progress bar attached to an image carousel. My carousel of choice is always Slick Slider .

After a quick search, I realised there probably wasn’t an out of the box solution, so I decided to build one using some basic jQuery and utilising some simple CSS.

This is all very simple stuff, so feel free to copy this into your project if you need it.

HTML <div class="progress" role="progressbar" aria-valuemin="0" aria-valuemax="100">
<span class="progress__true"></span>
<span class="slider__label sr-only">
</div>

For accessibility reasons, notice we’ve added the role attribute, aria attributes (valuemin and valuemax) to help for screen readers as well as the label span which won’t be visible to normal users.

The .progress__true span will act as the completed section of the bar.]

CSS ( or in this case some SCSS) .progress {
display: flex;
box-shadow: inset 0 1px 2px rgba(0,0,0,.1);
border-radius: 10px;
overflow: hidden;
background-color: #f5f5f5; &__true {
background-color: black;
min-width: 5%;
transition: all .4s ease-in;
height: 10px;
}
}

The CSS is also very simple.

The box shadow, and border radius ( as well as overflow-hidden ) ensures the element looks a little more like a progress bar, and the background colour is needed for the completed bar to stand out.

Adding a simple transition to the completed bar will improve it’s appearance. It’s also a good idea to add a min-width to this element, so that the progress bar doesn’t appear broken whilst the user is waiting for any javascript to load.

jQuery function calcProgress( count, current ) {
var calc = ( ( current + 1 ) / count ) * 100;
var notCompleted = 100 - calc;
$( '.progress__true' ).css( 'width', calc + '%' );
$( '.progress' ).attr('aria-valuenow', calc );
$( '.slider__label' ).text( calc + '% completed' );
};

The above function handles the progress bar calculations.

We’ll need to pass through two variables into the function:

The number of slides The current slide number

Having these two pieces of information will help us calculate the the progress of the carousel.

Slick Slider makes gathering this information really easy…

var count = slick.slideCount;
var current = slick.currentSlide;

Once we have that information, we need to add the width to the completed progress bar element, as well as adding some helpful screen reader attributes and labels.

The aria-valuenow attribute should be added to the progress bar itself, and adding the text to the hidden span will help those using screen readers.

We now need to set this function to run at two different times:

On initialisation of the carousel When the carousel changes slide

To do this we can use the following events…

$('.slider').on('init', function(event, slick) {
// the init event must be declared before the
// carousel
var count = slick.slideCount;
var current = slick.currentSlide;
calcProgress( count, current );
});

Using the on ‘init’ event will fire this once the carousel loads to give us the initial progress bar values.

It’s important to note that we must declare this event BEFORE the slick carousel itself.

We also need to make sure the progress bar updates as the carousel rotates the slides. To do this we can use the ‘afterChange’ event.

$('.slider').on('afterChange', function(event, slick) {
var count = slick.slideCount;
var current = slick.currentSlide;
calcProgress( count, current );
}); The Completed ProgressBar

I’ve saved the above code in CodePen.

Let me know your thoughts, or if there is a better way of achieving the same goal.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: CSSjQueryJavaScriptJavaHTML
分页:12
转载请注明
本文标题:Building a Simple Progress Bar for Slick Slider
本站链接:http://www.codesec.net/view/483285.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(47)