代码之家  ›  专栏  ›  技术社区  ›  Breezer

获取当前索引jquery

  •  1
  • Breezer  · 技术社区  · 14 年前

    嗨,我用的是 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 回复  |  直到 8 年前
        1
  •  2
  •   Doug Neiner    14 年前

    这个脚本的工作方式是,它不断移动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);
    });
    
        2
  •  1
  •   user229044    14 年前

    你可以使用 .index() 正是为了这个目的。