tags:

views:

22

answers:

1

I am trying to re-order the z-indexes of several DIVs so that the most recently dragged stays topmost.
I have the following code:

$(".elementsIWantToDrag").draggable({
    zIndex: 10000,
    stop: function(){
        $(this).css('z-index', 9999);
        alert($(this).css('z-index'));
    }
});

...and all is well and good until the actual execution. When I mouse-up after dragging an item, sure enough I get the alert with 9999 as the z-index value. Immediately after I click "OK" to clear the alert box, FireBug shows the value being changed to 10.

It appears to me that the stop() event is being called before the draggable code is actually finished doing everything... like setting the z-index back to what it was before the dragging started.

Removing the zIndex: 10000 seems to have a slightly different effect - draggable assigns different numbers to the z-indexes than with that parameter, but it is still overriding what I'm trying to do.

Suggestions on how to get this to work?

A: 

You could possibly circumvent this issue by changing the z-index of the object on a mouseover (for an x-windows follow focus type affair), or mousedown event (so that it fires just before the draggable z-index kicks in) that is bound to the '.elementsIWantToDrag'. This could then occur before the draggable event starts, and so it would 'revert' the z-index back to whatever you set it to in the mouse down event, as opposed to what it is being set to currently.

John Wordsworth
doesn't work - apparently it does some dynamic thinking and decides what the z-index should be. Grrrr.
ScottSEA
I've given this a little more thought, and I wonder if you could add the class 'lastMoved' on the dropped item during the stop function of the draggable configuration, and remove this class from all other elements on the page. Then setup 'mouseMove' on that class to raise that classes z-index. It would result in a pause until the mouse moves 1 pixel, but I can't think of a better way. Either that, or you could start off a 'setTimeout(*, 1)' during the stop function to alter the z-index of the dropped item. It would result in a minor delay after dropping the item and it moving to front though.
John Wordsworth