获取当前索引jquery

时间:2010-09-23 13:17:16

标签: jquery jquery-selectors fadein

嗨我正在使用的是一个脚本 http://snook.ca/archives/javascript/simplest-jquery-slideshow 基本上我现在的代码是

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

});

我想知道是否有可能获得当前显示的图像的索引?

2 个答案:

答案 0 :(得分: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);
});

答案 1 :(得分:1)

您可以将.index()用于此目的。

相关问题