网络科技

    今日:650| 主题:244663
收藏本版
互联网、科技极客的综合动态。

[其他] Building a Simple Progress Bar for Slick Slider

[复制链接]
梦转千遍如若初见 发表于 2016-10-16 07:09:23
105 4

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

Building a Simple Progress Bar for Slick Slider-1 (completed,solution,readers,decided,develop)
        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

  [code]
    
[/code]  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 )

  [code].progress {  display: flex;  box-shadow: inset 0 1px 2px rgba(0,0,0,.1);  border-radius: 10px;  overflow: hidden;  background-color: #f5f5f5;[/code]  [code]&__true {    background-color: black;    min-width: 5%;    transition: all .4s ease-in;    height: 10px;  }}[/code]  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

  [code]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' );};[/code]  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…
  [code]var count = slick.slideCount;var current = slick.currentSlide;[/code]  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…
  [code]$('.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 );});[/code]  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.
  [code]$('.slider').on('afterChange', function(event, slick) {  var count = slick.slideCount;  var current = slick.currentSlide;  calcProgress( count, current );});[/code]  The Completed Progress Bar

  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.
友荐云推荐




上一篇:新手指南:DVWA-1.9全级别教程之Brute Force
下一篇:Sanic – Python 3.5+ web server that's written to go fast
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

寄瑶 发表于 2016-10-16 08:46:31
欢乐,不颂~
回复 支持 反对

使用道具 举报

wo_shi_shui 发表于 2016-10-16 12:25:39
OMG!介是啥东东!!!
回复 支持 反对

使用道具 举报

eqxygmqrny 发表于 2016-10-22 12:23:28
楼上的这是啥态度呢?
回复 支持 反对

使用道具 举报

phpsir 发表于 2016-10-22 12:28:54
在撸一遍。。。
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表