Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select DIV with highest z-index

Example code so I can start explaining this problem:

<div style="z-index:5">5</div>
<div style="z-index:2">2</div>
<div style="z-index:1">1</div>
<div style="z-index:4">4</div>
<div style="z-index:3">3</div>

(z-index values do not matter, and their order, even less. Just example code)

Problem: I want to select (either using CSS or JS with jQuery) the DIV with highest z-index value. In the case above, I want to select the first one, because it's z-index 5 is higher than all the others' z-indexes.

Is there a way to do this? Extra information: I'm writing a Simple Window Manager with jQuery + jQuery UI, and the z-indexes are assigned by the stack option in .draggable. I can't seem to find a way to make jQuery to assign the last dragged element a class either, so I'm going by the highest z-index way. Any help please? Thanks.

like image 993
Jimmie Lin Avatar asked Jan 05 '10 11:01

Jimmie Lin


People also ask

What is the highest possible Z index?

The maximum range is ±2147483647. In CSS code bases, you'll often see z-index values of 999, 9999 or 99999.

What is Z Index 1000 in CSS?

z-index defines which positioned element appears on top (Sort of like layers). So z-index: 1000 would appear on top of a z-index 999 . Doing z-index: 1001 is an an attempt to appear "on top" of a item with z-index: 1000.

Is a higher z index better?

The z-index property determines the stack level of an HTML element. The “stack level” refers to the element's position on the Z axis (as opposed to the X axis or Y axis). A higher value means the element will be closer to the top of the stacking order.


1 Answers

I'm sure you could use the stop event to gain access to the recently dragged item, i.e.:

$('.selector').draggable({
   stop: function(event, ui) {
      $(event.target).addClass('justDragged');
   }
});

If you wish to see all functions/variables bound to event, you could use the following:

var str = '';
for (i in event) {
    str += i + ', ';
}
alert(str);

This should give you a good indication of what's available to you in any number of jQuery callback params.

like image 187
Corey Ballou Avatar answered Oct 20 '22 00:10

Corey Ballou