jquery - jquery 获取当前索引

标签 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);

});

我想知道是否可以获取当前显示的图像的索引?

最佳答案

此脚本的工作方式是不断移动 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);
});

关于jquery - jquery 获取当前索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3778734/

相关文章:

javascript - 在 jQuery UI 日期选择器中格式化日期

jquery - 查找 id 不包含某些字符的所有文本框

javascript - jQuery OR 运算符?

javascript - Jquery .load(),填充输入不起作用

javascript - 使用jquery使图像从左到右显示

jquery - 使用 Jquery + Accordion 淡入属性

javascript - jQuery .click() 方法未按预期运行

MVC 问题中的 JQuery Slider

当鼠标在图像上快速移动时触发 jquery mouse over 和 out 事件

javascript - 所选索引更改后 Jquery 数据表丢失 dropdownlist asp.net 更新面板