【问题标题】:jQuery UI Sortable: tolerance "intersect" doesn't work as expected (bug #8342)jQuery UI 可排序:容差“相交”不能按预期工作(错误 #8342)
【发布时间】:2013-03-27 16:27:42
【问题描述】:

9 天前(在撰写本文时)以下错误已重新打开: Sortable: Incorrect behaviour (or incorrect documentation) of sortable option tolerance: 'intersect'

很遗憾,我已经等不及 jQuery 来解决这个问题了。

我有一个容器,其中包含可以垂直排序的项目(全部为<div>s)。这些项目有不同的高度(这些高度都不是预定义的)。

有没有合适的解决方法?

【问题讨论】:

    标签: jquery-ui jquery-ui-sortable


    【解决方案1】:

    我自己写了一个解决方法,灵感来自dioslaska's solution to his own question here: jQuery UI sortable tolerance option not working as expected

    它运行得非常顺利:)

    去掉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;
                    }
                }
            }
        });
    }
    

    【讨论】:

    • 请注意:sort 被非常频繁地调用,因此在每次鼠标移动时遍历容器的所有子容器可能代价高昂..
    猜你喜欢
    • 2012-05-25
    • 1970-01-01
    • 2022-08-17
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多