how to get the total number of slides in flexslider
Asked Answered
I

6

7

I am using flexslider http://www.woothemes.com/flexslider/ , now I want to get the total number of the slides and the number of the sildes.

number of the slide/total number of the slides

for example if I have 5 slides and I am now in 2nd slide, so this will be the output below the slides.

2/5

if I click the "next nav" it will become

3/5

if "prev nav";

2/5

It is possible in flexslider? if yes, how to do it?

Irritating answered 1/2, 2013 at 2:57 Comment(0)
C
4

Base on the demo here

I notice the demo and others will generate the code like below :

<ol class="flex-control-nav flex-control-paging">
    <li><a class="">1</a>
    </li>
    <li><a class="flex-active">2</a>
    </li>
    <li><a>3</a>
    </li>
    <li><a>4</a>
    </li>
</ol>

So according this, you can get what you want using the code:

var index = $('li:has(.flex-active)').index('.flex-control-nav li')+1;
var total = $('.flex-control-nav li').length;
Candent answered 1/2, 2013 at 3:20 Comment(0)
T
11

You can find the answer here:http://www.woothemes.com/flexslider.

    <script type="text/javascript" charset="utf-8">
      $(window).load(function() {
        $('.flexslider').flexslider({
          animation: "slide",
          controlsContainer: ".flex-container",
          start: function(slider) {
            $('.total-slides').text(slider.count);
          },
          after: function(slider) {
            $('.current-slide').text(slider.currentSlide);
          }
       });
     });
    </script>

If you want even the first slide to be counted you can add in the start function:

$('.current-slide').text(slider.currentSlide);

If you want the current-slide to begin with number 1 (not 0) you can do:

$('.current-slide').text(slider.currentSlide+1);
Tetramethyldiarsine answered 26/8, 2013 at 16:8 Comment(1)
This is the best way.Kirksey
O
10

According to this website, you can do it with the callback API. http://www.woothemes.com/flexslider/. Write a Start and After callback method when you instantiate flexslider, and pass in the slider. Then use slider.count and slider.currentSlide to get what you need. In my code below, $slider_wrap, $current_slide and $total_slides are just variables assigned to jQuery objects where I wanted to display the slider count. I did slider.currentSlide+1 because the first slide is actually 0 in the array.

$slider_wrap.flexslider({
    start: function(slider){
      $current_slide.html(slider.currentSlide+1);
      $total_slides.html(slider.count);
    },
    after: function(slider){
      $current_slide.html(slider.currentSlide+1);
      $total_slides.html(slider.count);
    }
  });
Osgood answered 12/4, 2013 at 14:39 Comment(0)
C
4

Base on the demo here

I notice the demo and others will generate the code like below :

<ol class="flex-control-nav flex-control-paging">
    <li><a class="">1</a>
    </li>
    <li><a class="flex-active">2</a>
    </li>
    <li><a>3</a>
    </li>
    <li><a>4</a>
    </li>
</ol>

So according this, you can get what you want using the code:

var index = $('li:has(.flex-active)').index('.flex-control-nav li')+1;
var total = $('.flex-control-nav li').length;
Candent answered 1/2, 2013 at 3:20 Comment(0)
B
1

Based on the markup in that page you link you can get the number of slides like this:

$(".slides").find("li").length

And you can find the number of the active slide with:

$(".slides").find("li.flex-active-slide").index() + 1;

And if you want to change something when the slider changes, you can add something to the after callback.

$(".mybox").flexslider({
  after: function() {
    // update the count
  }
});
Bamberg answered 1/2, 2013 at 3:16 Comment(1)
$(".slides").find("li") will find too many li which is not OP wanting.Candent
S
0

I am currently doing exactly the same thing to generate the slide index (...though I'm using PHP to generate an integer for the number of images)

Try creating an element for the slide index...

<p class="slideIndex"></p>

...and use the after:function...

$('.flexslider').flexslider({
    after: function(slider) {
        slider.find('.slideIndex').html(slider.currentSlide + 1);
    }
});
Shing answered 21/8, 2013 at 13:2 Comment(0)
R
0

Get the slider and then use count:

$(yourslider).data('flexslider').count

Refreshment answered 27/8, 2015 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.