开发者

Looping through elements and adding a decreasing z-index

I'm trying to stack a group of elements in decreasing order with z-index.

So for a collection of three li elements they would look like:

<li style="z-index:3"></li>
<li style="z-index:2"></li>
<li style="z-index:1"></li>

So far i've found the method explained here Looping through a collection of li elements adding a z-index and incrementing value on each seems very close:

$('#contentS开发者_如何学编程lider ul li').each(function(i, el) {
   $(el).css('z-index', i + 1);
});

And at this stage im guessing i can use $("#contentSlider ul li").length; in some way to count down with ('z-index', i - 1); but beyond that I'm stuck.


You seem to know how to do it already... Maybe this is what you want?

var lis = $('#contentSlider ul li');
lis.each(function(i, el){
   $(el).css('z-index', lis.length-i);
});


Your guess is absolutely correct. You can do:

var zIndex = $('#contentSlider ul li').length;
$('#contentSlider ul li').each(function(i, el) {
   $(el).css('z-index', zIndex);
   zIndex--;
});

Here is a working example: http://jsfiddle.net/HVs6N/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜