2010-09-23 33 views

回答

2

該腳本的工作方式是,它會一直移動DOM元素的位置,以便您當前的img始終爲索引0,而下一個img始終爲索引1.如果您想知道原始順序中的索引,您需要在幻燈片腳本運行之前存儲該數據:

$(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); 
}); 
+0

這工作完美! – Breezer 2010-09-23 14:01:20

1

您可以使用.index()來達到這個目的。

+0

是的但我試過$('。fadein img')。index();但那不起作用,那麼我將如何使用它? – Breezer 2010-09-23 13:21:44

+0

@Breezer:你應該看看meagars文章中提供的文檔鏈接。 – jwueller 2010-09-23 13:24:32

+0

使用'.next('img')。index()'獲取下一個img元素的索引。 – meagar 2010-09-23 13:25:31