Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse z-index on stacked divs

I have multiple columns on a page wrapped in divs this way:

<div class="first column" style="width:33%; float: left;"></div>
<div class="column" style="width:33%; float: left;"></div>
<div class="last column" style="width:33%; float: left;"></div>

I'm basically using a plugin that columnizes text and positions them left to right. In my css, I reposition them so they stack on top of each other, and then I use some jquery to move each column out of the way. Problem is, they're stacking in the wrong order. The last column is frist, the first column is on the bottom.

Here's the CSS

#reader .column {
    position:absolute;
    top:0;
    left:0;
    background:#fff;
}

What can I do to change the stacking order? Is the best solution to use jQuery to add a z-index to each div? I'm not entirely how to do that, JS is not my strength. That also seems kind of brittle to me. Is there a way to simply reverse the stacking order? Here's my very simple jQuery:

$(function(){
    $('#reader-inner').columnize();
    $('.column').click(function() {
      $(this).clearQueue().animate({left:'-550'},500, 'swing');
    });
});
like image 433
Slick23 Avatar asked Nov 25 '22 22:11

Slick23


1 Answers

Here's what I ended up with:

$('.column').each(function(i, c) {
        $(c).css('z-index', num - i);
    });

Where num is actuall the number of columns on the page, plus an arbitrary amount based on the highest z-index in the other elements.

like image 131
Slick23 Avatar answered Dec 10 '22 00:12

Slick23