Sortable with Z-index

Asked

Viewed 31 times

0

People I’m wanting to use this function that Jquery provides but increasing it:

Jquery function:

$(function() {
   $("#sortable").sortable();
   $("#sortable").disableSelection();
});
#sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
#sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }
#sortable li span { position: absolute; margin-left: -1.3em; }
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    
<ul id="sortable">
    <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 1</li>
    <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 2</li>
    <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 3</li>
    <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 4</li>
    <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 5</li>
    <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 6</li>
    <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 7</li>
</ul>

What I want to do when dragging the list item up an item linked to this list gets z index higher

This other code does something a little bit like:

$(document).ready(function() {
   var a = 3;
   $('#box1,#box2,#box3,#box4').draggable({
      start: function(event, ui) { $(this).css("z-index", a++); }
   });
   $('#dragZone div').click(function() {
      $(this).addClass('top').removeClass('bottom');
      $(this).siblings().removeClass('top').addClass('bottom');
      $(this).css("z-index", a++);        
   });
});
#box1 {width: 150px; height: 150px; background-color: red; position: absolute; top: 0px; left: 0px; z-index: 0}
#box2 {width: 150px; height: 150px; background-color: green; position: absolute; top: 20px; left: 50px; z-index: 0}
#box3 {width: 150px; height: 150px; background-color: yellow; position: absolute; top: 50px; left: 100px; z-index: 0}
#box4 {width: 150px; height: 150px; background-color: blue; position: absolute; top: 70px; left: 200px; z-index: 0}
.top {z-index: 2; position: relative}
.bottom {z-index: 1; position: relative}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
     
<div id="dragZone">
    <div id="box1"></div>
    <div id="box2"></div>
    <div id="box3"></div>
    <div id="box4"></div>
</div>

But it puts the Z-index when I drag and on the very item I dragged. I would be very grateful if someone could find the solution to this challenge and share.

  • You don’t want z-index to work? vc want to remove and maintain the original z-index of the elements without changing z-index while dragging or dropping?

  • I want to make a List like the one in First example and when dragging up an item, another gets z-index higher

  • It’s kind of like this if Item 2 goes to the top id="undefined" gets z index higher than item 1 that went down it in the list. Only not the Items that will receive Z-index but other elements within html

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.