I know this seems strange because there already is an update event. But I'm using the touch-punch
library to make jQuery UI Functions work on touch devices. But for example on Nexus 7's Chrome browser the update event doesn't fire. That's why I try to fix it with triggering the sortupdate
event from the sortover
event.
The problem is, that I don't know how to get the index of the element below the dragged element. ui.item
only returns the index of the dragged element. Is there any jQuery UI inbuilt function for this? Getting the index by mouseover/hover checks wouldn't work because the dragged element is bigger than the mousecursor
. Getting the elements index by cursor position also seems very tricky...
Here is a fiddle: https://jsfiddle.net/8sjLw6kL/2/
Code: HTML:
<div id="sortable">
<div class="sortable"> bla </div>
<div class="sortable"> ble </div>
<div class="sortable"> blu </div>
</div>
JS:
var start_sort_index,
over_sort_index,
update_sort_index;
$('#sortable').sortable({
helper: 'clone',
containment: 'parent',
cursor: 'move'
});
$('#sortable').on('sortstart', function(event, ui){
start_sort_index = ui.item.index();
console.log('start: '+start_sort_index);
});
$('#sortable').on('sortover', function(event, ui){
over_sort_index = ui.item.index();
console.log('over: '+over_sort_index);
$('#sortable').trigger('sortupdate');
});
$('#sortable').on('sortover', function(event, ui){
update_sort_index = (typeof ui !== 'undefined')?ui.item.index():over_sort_index;
over_sort_index = undefined;
//my other code here
});
CSS:
#sortable{
width: 100%;
background-color: #ebebeb;
position: relative;
margin-top: 10px;
height: 60px;
}
.sortable{
padding: 5px 10px;
background-color: red;
margin: 5px;
float: left;
}