繁体   English   中英

每个列表项的增量

[英]Increment for each list item

我试图增加每个列表项的z-index

这是我到目前为止尝试过的:

    var photos = $('ul');
  photos.each(function () {

      var photos = $(this).children();
      var photosLen = photos.length;
      if (photosLen > 1) {
          photos.parent().addClass('album');
          var i = 0;
          while (i < photosLen) {

              $(this).children().css({
                  'z-index': i
              });
          }
      }

  });

我希望每个列表项都可以来自z-index: 1; z-index: 3; 但是它没有那样做。 只需将数组的长度添加到每个列表项。

HTML :(代码仅适用于第一个无序列表)

<ul>

    <li><img src="http://i.imgur.com/PuwwFs.jpg" alt=""></li>
    <li><img src="http://i.imgur.com/cjAGks.jpg" alt=""></li>
    <li><img src="http://i.imgur.com/zA4lCs.jpg" alt=""></li>

  </ul>
    <ul>

        <li><img src="http://i.imgur.com/PuwwFs.jpg" alt=""></li>


    </ul>

由于您已经在使用jQuery,因此代码可能很简单

  if (photosLen > 1) {
      photos.each(function(i) {
          $(this).css('zIndex', i);
      })
      .parent()
      .addClass('album');
  }

注意z-index属性的each()和camelCase语法

您忘记在代码中增加i的值:

      while (i < photosLen) {

          $(this).children().css({
              'z-index': i
          });
          i++; 
      }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM