Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

javascript - Display number of slides as owl carousel's pagination?

I'm creating a slider using the Owl Carousel. I'd like to display something like "1 of number of slides" and increment the number on each slide. I've tried a couple of methods using jQuery but they haven't worked.

HTML:

<div class='owl-carousel owl-theme'>
  <div class='item'>
    <span class='article-card-number'></span>
  </div>
  <div class='item'>
    <span class='article-card-number'></span>
  </div>
  <div class='item'>
    <span class='article-card-number'></span>
  </div>
  <div class='item'>
    <span class='article-card-number'></span>
  </div>
  <div class='item'>
    <span class='article-card-number'></span>
  </div>
  <div class='item'>
    <span class='article-card-number'></span>
  </div>
</div> 

jQuery:

$('.owl-carousel').owlCarousel({
    loop:true,
    margin:20,
    nav:true,
    responsive:{
          0:{
              items:1
          },
          600:{
              items:2
          },
          1000:{
              items:3
          }
      },
  });

  var numberOfSlides = $('.item').length;
        
  for (var i = 0; i < numberOfSlides; i++) {
    $('.article-card-number').html(i + ' of ' numberOfSlides);
  }

Any help would be greatly appreciated, thanks.

question from:https://stackoverflow.com/questions/65922165/display-number-of-slides-as-owl-carousels-pagination

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have two options:

With events:

$('.owl-carousel').owlCarousel({
    // your initialisation code
});

owl.on('translated.owl.carousel', function(event) {
    $('.article-card-number').text(event.item.index + ' of ' event.item.count);
});

With info:

$('.owl-carousel').owlCarousel({
    ...
    info: getInfo
});

function getInfo(i){
    $('.article-card-number').text(i.currentPage + ' of ' i.allPages);
}

Note: None of the code segments above were tested but should work or require minimal changes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...