javascript - 如何为 Owl Carousel 2 创建进度条?

标签 javascript jquery html css owl-carousel-2

官老link Owl 1 的进度条甚至不再工作,但我发现它可以工作 example也适用于猫头鹰 1。

我已尝试使用该代码,但无法将其设置为与 Owl 2 一起使用 http://codepen.io/anon/pen/GrgEaG

$(document).ready(function() {

  var time = 7; // time in seconds

  var $progressBar,
      $bar, 
      $elem, 
      isPause, 
      tick,
      percentTime;

    //Init the carousel
    $("#owl-demo").owlCarousel({
      items: 1,
      initialized : progressBar,
      translate : moved,
      drag : pauseOnDragging
    });

    //Init progressBar where elem is $("#owl-demo")
    function progressBar(elem){
      $elem = elem;
      //build progress bar elements
      buildProgressBar();
      //start counting
      start();
    }

    //create div#progressBar and div#bar then prepend to $("#owl-demo")
    function buildProgressBar(){
      $progressBar = $("<div>",{
        id:"progressBar"
      });
      $bar = $("<div>",{
        id:"bar"
      });
      $progressBar.append($bar).prependTo($elem);
    }

    function start() {
      //reset timer
      percentTime = 0;
      isPause = false;
      //run interval every 0.01 second
      tick = setInterval(interval, 10);
    };

    function interval() {
      if(isPause === false){
        percentTime += 1 / time;
        $bar.css({
           width: percentTime+"%"
         });
        //if percentTime is equal or greater than 100
        if(percentTime >= 100){
          //slide to next item 
          $elem.trigger('owl.next')
        }
      }
    }

    //pause while dragging 
    function pauseOnDragging(){
      isPause = true;
    }

    //moved callback
    function moved(){
      //clear interval
      clearTimeout(tick);
      //start again
      start();
    }

    //uncomment this to make pause on mouseover 
    // $elem.on('mouseover',function(){
    //   isPause = true;
    // })
    // $elem.on('mouseout',function(){
    //   isPause = false;
    // })

});

#bar{
  width: 0%;
  max-width: 100%;
  height: 4px;
  background: #7fc242;
}
#progressBar{
  width: 100%;
  background: #EDEDED;
}

最佳答案

回调函数没有被触发,因为您在 owlCarousel 2 中不存在的事件上调用它们。事件以“on”为前缀。

所以如果你这样称呼他们:

$("#owl-demo").owlCarousel({
  items: 1,
  onInitialized : progressBar,
  onTranslate : moved,
  onDrag : pauseOnDragging
});

函数将被调用。查看 owlCarousel 事件文档 here .

查看 this CodePen使用 CSS 转换的 OwlCarousel 中的示例进度条。

关于javascript - 如何为 Owl Carousel 2 创建进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41481066/

相关文章:

javascript - 发送文件给客户端下载

javascript - jQuery .append() 字符串被 IE7 html 解析器乱码

javascript - 一页网站的滚动效果

html - IE8高度100%的BUG

jquery - 当摘录超过一定限制时添加底部填充

javascript - IE localStorage 事件失败

javascript - 如何显示查询回调数据结果只以开头

javascript - Highcharts 将小数值四舍五入为 0。如何避免这种情况?

javascript - jQuery DataTable 多行表头

html - 如何选择具有特定类的最后一个元素,而不是父元素中的最后一个子元素?