jquery 获取当前索引

发布于 2024-09-24 19:05:44 字数 438 浏览 0 评论 0原文

嗨,我正在使用由 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);

});

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

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-child').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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

影子是时光的心 2024-10-01 19:05:44

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

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);
});
箹锭⒈辈孓 2024-10-01 19:05:44

您可以使用 .index() 来达到此目的。

You can use .index() for exactly that purpose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文