views:

29

answers:

2

hi i'm using a script made by http://snook.ca/archives/javascript/simplest-jquery-slideshow so basically the code i have now is

$(function(){
$('.fadein img:gt(0)').hide();
setInterval(function(){
  $('.fadein :first-child').fadeOut()
     .next('img').fadeIn()
     .end().appendTo('.fadein');
  }, 
  6000);

});

I wonder if it's possible to get the index of the current image that shows in anyway? all help is very much appreciated Regards

+1  A: 

You can use .index() for exactly that purpose.

meagar
yes but i tried $('.fadein img').index(); but that didnt work so how would i use it?
Breezer
@Breezer: You should take a look at the documentation-link provided in meagars post.
elusive
Use `.next('img').index()` to get the index of the next img element.
meagar
I looked through the documentation and tried if i havent missed something pretty much everything when i try to alert the value out most of the times i get -1 or it outputs 0 for all the images
Breezer
lets saay you would like to output the current index how would you approach this?
Breezer
@Breezer The current index of what, and in what context? Are you looking for the last-appended image in the `.fadeIn` container? You can do that much more simply with `$('.fadeIn :last-child')`. Or, since it's likely the only visible image, you can do '$('.fadeIn img:visible')`.
meagar
+2  A: 

The way this script works, it keeps moving the position of the DOM elements so that your current img is always index 0 and the next img is always index 1. If you want to know the index out of your original order, you will need to store that data before the slideshow script runs:

$(function () {
   $(".fadein img").each(function (i, el) {
      $(el).data('index', i); // Store the index on the IMG
   });

   $('.fadein img:gt(0)').hide();

   setInterval(function(){
      $('.fadein :first-child').fadeOut()
      .next('img').fadeIn()
      .end().appendTo('.fadein');

      // Get the original index of the first img in the current show
      alert($('.fadein :first-child').data('index'));
   }, 6000);
});
Doug Neiner
this worked perfect !
Breezer