mmdwc
mmdwc

Reputation: 1137

adding counter to a jquery slideshow

I'm using a slideshow I've built with Javascript for my website. I would like to add a counter.

1 of 3...

slide 1 / total slide...

I can't manage to find the solution...

here is my code :

$(document).ready(function() {
    // set display:none for all members of ".pic" class except the first
    $('.image_news:gt(0)').hide();

    // stores all matches for class="pic"
    var $slides = $('.image_news');

    $slides.click(function(){
        // stores the currently-visible slide
        var $current = $(this);    
        if( $current.is($slides.last()) ) {
            $current.hide();
            $slides.first().show();
        } 
        // else, hide current slide and show the next one
        else {
            $current.hide().next().show(); 
        }
    });
});

and a fsfiddle link with example : http://jsfiddle.net/XRpeA/3/

thanks a lot for your help !

Upvotes: 2

Views: 4059

Answers (5)

mmdwc
mmdwc

Reputation: 1137

thanks a lot. I'll use the first option, @Hiral. I have another slideshow in my website and when trying the other options it's not working fine... but thanks a lot everyone !

I have another thing to fix. when I have 2 or 3 slideshow on my page, it doesn't work... I'm using a repeater custom fields in my admin page, so I'm not able to know in advance how many slideshow I will need... does anyone knows how I can fix it ? here is a jsfiddle : http://jsfiddle.net/XRpeA/13/

<div id="slideframe">
    <img class="image_news" src="http://s1.lemde.fr/image/2007/07/09/534x0/933544_5_aa6d_polynesie-bora-bora_0608bcead896ce3f59fc0e2fb3cc7435.jpg" />
    <img class="image_news" src="http://production.slashmedias.com/main_images/images/000/005/357/IMAGE-PENSEE-HD-1_original_original_large.jpg?1372235419" />
    <img class="image_news" src="http://images.telerama.fr/medias/2013/03/media_94814/une-image-un-film-l-auberge-de-dracula-1931,M106823.jpg" />
</div>
<br>
    <div id="counter">image <span id="current">1</span> / <span id="total"></span></div>

 <br><br>

<div id="slideframe">
<img class="image_news" src="http://s1.lemde.fr/image/2007/07/09/534x0/933544_5_aa6d_polynesie-bora-bora_0608bcead896ce3f59fc0e2fb3cc7435.jpg" />
<img class="image_news" src="http://production.slashmedias.com/main_images/images/000/005/357/IMAGE-PENSEE-HD-1_original_original_large.jpg?1372235419" />
<img class="image_news" src="http://images.telerama.fr/medias/2013/03/media_94814/une-image-un-film-l-auberge-de-dracula-1931,M106823.jpg" />
</div>
<br>
<div id="counter">image <span id="current">1</span> / <span id="total"></span></div>

thanks a lot

Upvotes: 1

Marvin Smit
Marvin Smit

Reputation: 4108

Another option

    // stores the currently-visible slide
    var $current = $(this);
    $current.hide();
    if( $current.is($slides.last()) ) 
    {
        $current = $slides.first();
    } 
    // else, hide current slide and show the next one
    else 
    {
        $current = $current.next();
    }
    $current.show();
    $('#counter').text('image ' + ($slides.index($current) + 1) + ' / ' + $slides.length);

Plenty of options to choose from

Upvotes: 0

j0hnstew
j0hnstew

Reputation: 709

Just add a counter and change the html. Here is a Fiddle.

var counter = 1;
$("#counter").html("image "+counter+"/3");
$slides.click(function(){
    // stores the currently-visible slide
    var $current = $(this);    
    if( $current.is($slides.last()) ) {
        $current.hide();
        $slides.first().show();
        counter = 1;
        $("#counter").html("image "+counter+"/3");
    } 
    // else, hide current slide and show the next one
    else {
        counter++;
        $current.hide().next().show(); 
        $("#counter").html("image "+counter+"/3");
    }
});

Upvotes: 1

codingrose
codingrose

Reputation: 15699

Write this:

var count = $('.image_news').length; //length gives total length of elements
$("#total").text(count);

if ($current.is($slides.last())) {
    $("#current").text("1");//first slide
    $current.hide();
    $slides.first().show();
}
// else, hide current slide and show the next one
else {
    $("#current").text($current.next().index()+1); 
    //index() returns index, add 1 because it starts from 0
    $current.hide().next().show();
}

Fiddle here.

Upvotes: 2

SRay
SRay

Reputation: 286

Give each image an id eg:1,2,3 and display the id when you are on that image.

Upvotes: 0

Related Questions