get current index jquery
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-c开发者_运维问答hild').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?
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);
});
You can use .index()
for exactly that purpose.
精彩评论