【发布时间】:2016-04-01 10:51:48
【问题描述】:
我知道这看起来很奇怪,因为已经有一个更新事件。但是我使用touch-punch 库来使jQuery UI 函数在触摸设备上工作。但例如在 Nexus 7 的 Chrome 浏览器上,更新事件不会触发。这就是为什么我尝试通过从sortover 事件触发sortupdate 事件来修复它。
问题是,我不知道如何获取被拖动元素下方元素的索引。 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;
}
【问题讨论】:
-
进入任何可拖动元素的经典方法是
.hidehelper,得到鼠标的.elementFromPoint,然后再次.showhelper。但是,在这种情况下,最好先弄清楚为什么没有触发更新后处理程序 -
感谢您的提示!我希望我知道为什么 sortupdate 事件有时不会触发,但我还想不通。 “elementFromPoint”函数无法轻松完成这项工作,因为光标不在 div 重叠的位置,因此即使将指针事件设置为无并且 ui-sortable 设置为指针,它也主要选择父元素 -事件:自动。到达重叠位置的计算有点复杂。 ...
-
...指针事件在 IE 中也不起作用,并且在尝试获取位置时,触摸和鼠标之间也可能存在差异。对于一些可排序的元素来说太复杂了。 ;) 但无论如何感谢您的提示! :)
标签: jquery jquery-ui jquery-ui-sortable jquery-ui-touch-punch