我知道这看起来很奇怪,因为已经有一个更新事件。但我正在使用该touch-punch
库使 jQuery UI 函数在触摸设备上工作。但例如在 Nexus 7 的 Chrome 浏览器上,更新事件不会触发。这就是为什么我尝试通过sortupdate
从事件触发事件来修复它sortover
。
问题是,我不知道如何获取拖动元素下方的元素索引。ui.item
只返回被拖动元素的索引。是否有任何 jQuery UI 内置功能?通过鼠标悬停/悬停检查获取索引不起作用,因为拖动的元素大于mousecursor
. 通过光标位置获取元素索引似乎也很棘手......
这是一个小提琴: https ://jsfiddle.net/8sjLw6kL/2/
代码: 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;
}