jQuery UI 可排序:容差"intersect"无法按预期工作(错误 #8342)



9天前(撰写本文时)以下bug已重新打开:Sortable:可排序选项公差:'intersect'的错误行为(或错误文档)

不幸的是,我不能等待jQuery解决这个问题。

我有一个单独的容器,里面的项目可以垂直排序(所有的<div>),这些项目有不同的高度(这些高度都不是预定义的)。

是否有合适的解决方案可用?

我自己写了一个解决方法,灵感来自diosaska对他自己的问题的解决方案:jQuery UI可排序容差选项未按预期工作

它工作得很顺利:)

删除tolerance选项,使用以下函数作为sort选项:

function( e, ui ) {
    var container   = $( this ),
        placeholder = container.children( '.ui-sortable-placeholder:first' );
    var helpHeight  = ui.helper.outerHeight(),
        helpTop     = ui.position.top,
        helpBottom  = helpTop + helpHeight;
    container.children().each( function () {
        var item = $( this );
        if( !item.hasClass( 'ui-sortable-helper' ) && !item.hasClass( 'ui-sortable-placeholder' )) {
            var itemHeight = item.outerHeight(),
                itemTop    = item.position().top,
                itemBottom = itemTop + itemHeight;
            if(( helpTop > itemTop ) && ( helpTop < itemBottom )) {
                var tolerance = Math.min( helpHeight, itemHeight ) / 2,
                    distance  = helpTop - itemTop;
                if( distance < tolerance ) {
                    placeholder.insertBefore( item );
                    container.sortable( 'refreshPositions' );
                    return false;
                }
            } else if(( helpBottom < itemBottom ) && ( helpBottom > itemTop )) {
                var tolerance = Math.min( helpHeight, itemHeight ) / 2,
                    distance  = itemBottom - helpBottom;
                if( distance < tolerance ) {
                    placeholder.insertAfter( item );
                    container.sortable( 'refreshPositions' );
                    return false;
                }
            }
        }
    });
}

最新更新