Draggable element on top
HTML
<div id="redDiv" class="divClass" style="background-color: red">
Red Div
</div>
<div id="greeDiv" class="divClass" style="background-color: green">
Green Div
</div>
<div id="blueDiv" class="divClass" style="background-color: blue">
Blue Div
</div>
<div id="brownDiv" class="divClass" style="background-color: brown">
Brown Div
</div>
CSS
.divClass {
font-family: Arial;
height: 200px;
width: 200px;
color: white;
display: table-cell;
vertical-align: middle;
text-align: center;
z-index: 0;
}
jQuery
$(document).ready(function () {
$('.divClass').draggable();
});
To bring a Div Element that is being dragged on top of other draggable div's on the page, use stack option. The way this works is jQuery sets the z-index of the element that is being dragged to higher than the z-index of any other draggable div element on the page. You can see the value of the z-index in browser developer tools.
$(document).ready(function () {
$('.divClass').draggable({
stack: '.divClass'
});
});
However, if you simply click on a Div element without dragging then the element is not brought on to the top. Here is the code to bring the element on top as soon as mousedown event is triggered. All this function does is change the z-index of the div element to a value greater than the z-index of any other div element.
$(document).ready(function () {
$('.divClass').draggable();
$('.divClass').mousedown(function () {
var maxZindex = 0;
$(this).siblings('.divClass').each(function () {
var currentZindex = Number($(this).css('z-index'));
maxZindex = currentZindex > maxZindex ? currentZindex : maxZindex;
});
$(this).css('z-index', maxZindex + 1);
});
});
<div id="redDiv" class="divClass" style="background-color: red">
Red Div
</div>
<div id="greeDiv" class="divClass" style="background-color: green">
Green Div
</div>
<div id="blueDiv" class="divClass" style="background-color: blue">
Blue Div
</div>
<div id="brownDiv" class="divClass" style="background-color: brown">
Brown Div
</div>
CSS
.divClass {
font-family: Arial;
height: 200px;
width: 200px;
color: white;
display: table-cell;
vertical-align: middle;
text-align: center;
z-index: 0;
}
jQuery
$(document).ready(function () {
$('.divClass').draggable();
});
To bring a Div Element that is being dragged on top of other draggable div's on the page, use stack option. The way this works is jQuery sets the z-index of the element that is being dragged to higher than the z-index of any other draggable div element on the page. You can see the value of the z-index in browser developer tools.
$(document).ready(function () {
$('.divClass').draggable({
stack: '.divClass'
});
});
However, if you simply click on a Div element without dragging then the element is not brought on to the top. Here is the code to bring the element on top as soon as mousedown event is triggered. All this function does is change the z-index of the div element to a value greater than the z-index of any other div element.
$(document).ready(function () {
$('.divClass').draggable();
$('.divClass').mousedown(function () {
var maxZindex = 0;
$(this).siblings('.divClass').each(function () {
var currentZindex = Number($(this).css('z-index'));
maxZindex = currentZindex > maxZindex ? currentZindex : maxZindex;
});
$(this).css('z-index', maxZindex + 1);
});
});
Comments
Post a Comment