[英]Increment z-index for each div
I am working on a design where divs overlay each other and I do to separately increment z-index
by 1 for each member of a class.我正在设计一个 div 相互重叠的设计,我会为类的每个成员分别将z-index
增加 1。 And I feel like there is a better way to do it with jquery
.我觉得用jquery
有更好的方法来做到这一点。
What I want is something like for each div with the class section
to have a z-index
greater by than the previous but not being thrown off by divs which are not members of the specific class such as我想要的是每个带有类section
div 的z-index
大于前一个,但不会被不是特定类的成员的 div 抛弃,例如
<div class="section" style="z-index:1">Lorem</div>
<div class="section" style="z-index:2">Lorem</div>
<div id="OutOfPlaceDiv">Foo</div>
<div class="section" style="z-index:3">Lorem</div>
Use the each
method to iterate through the elements with that class. 使用each
方法迭代该类的元素。
$('.section').each(function(index) {
$(this).css("z-index", index);
});
This will set the indexes to 0, 1, 2
etc. If you want to start at something other than 0
: 这会将索引设置为0, 1, 2
等。如果要从0
以外的其他位置开始:
var offset = 1;
$('.section').each(function(index) {
$(this).css("z-index", offset + index);
});
A dirty css hack but simple and effective if you have just dozen of div:一个肮脏的 css hack 但如果你只有十几个 div,那么简单而有效:
.section:nth-child(1) { z-index: 1; }
.section:nth-child(2) { z-index: 2; }
.section:nth-child(3) { z-index: 3; }
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.