I am trying to set the z-index on draggable elements using jQuery. You can see what I am talking about and what I have so far here:
http://jsfiddle.net/sushik/LQ4JT/1/
It is very primitive and there are problems with it. Any ideas on how I would make the last clicked element have the highest z-index and rather than resetting all of the rest to a base z-index
, have them step, so the 2nd to last clicked has the second highest z-index
, etc..
Another problem I am having with it is that it only works on a full click event but the draggable functionality works by clicking and holding down. How could I have the class applied on that initial click down and not wait for the event of releasing the click?
All you need to do is use
draggable({stack: "div"})
Now when you drag a div it will automatically come to the top.Check working example at http://jsfiddle.net/LQ4JT/8/
Following code will fulfill your requirements. You need to
stack
yourdivs
instead of settingz-indexes
and secondly you need to show the div at top after simply clicking it not dragging it.So for stacking you need
stack: "div"
and for showing the div element on the top by simply click, you need to usedistance: 0
.By default the value is
distance: 10
which means until you don't drag it10 pixels
, it won't show up on the top. By setting the value todistance: 0
makes it show on the top after a simple click.Try the following code.
Working JSFiddle Here.
Edit:
Click the Run code snippet button below to execute this embedded code snippet.
Here is a greatly simplified version of Mahesh's answer:
http://jsfiddle.net/LQ4JT/713/
Still seems to work well, unless I am missing something.
The easiest way I found to solve this problem was to use the appendTo and zIndex options.
I have updated your CSS and Javascript. Don't use "!important" in css unless you are that much desperate.
http://jsfiddle.net/LQ4JT/7/
});
Though this answer works it has the limitation of max number of 2^31−1 in javascript. refer What is JavaScript's highest integer value that a Number can go to without losing precision? for more info.