jQuery UI可排序动画

Cha*_*had 9 jquery jquery-ui css3 jquery-ui-sortable

我有一个类似于网格的列表,并且具有可计算的可排序功能.我想为除了被操纵的项目之外的每个项目设置动画,以便在列表中平滑滑动.我在这里设置了一个示例:http://jsfiddle.net/wpmte/.

<ul id="sort">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
    <li>Item 5</li>
    <li>Item 6</li>
    <li>Item 7</li>
    <li>Item 8</li>
    <li>Item 9</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

CSS:

ul {
    margin: 0;
    padding: 0;
}
li {
    display: inline-block;
    margin: 5px;
    padding: 5px;
    background: #0f0;
    width: 25%;
}
.ui-sortable-placeholder {
    height: 0 !important;
}
Run Code Online (Sandbox Code Playgroud)

最后,JS:

$('#sort').sortable({ 

});
Run Code Online (Sandbox Code Playgroud)

如何通过过渡而不是仅仅跳跃来为元素添加动画来填充空间?

Bra*_*eid 2

我是这样做的:

// needed to allow for multiple placeholders as they animate
var placeholderNumber = 0;

$('#new-ct-banner-tree').sortable({
    // basic setup
    distance: 15,
    placeholder: 'tree-drop-placeholder',
    items: ".tree-item:not(.new-ct-tree-group-item, .group-add-button)",
    connectWith: ".connectedSortable",
    handle: ".top-drag-handle",
    helper: "clone",
    opacity: 0.75,
    revert: 300,
    scrollSpeed: 4,
    cursor: "move",

    start: function(event, ui) {
        // When starting the drag, add our replacement placeholder. We set the height of the default placeholder to 30px (see css below), so the replacement needs to be that same height and the original gets a negative margin of that height and our replacement consumes the original.
        $(ui.placeholder).before('<div class="temp-spacer-' + placeholderNumber +     '"></div>').css('margin-top', '-30px');
        $('.temp-spacer-' + placeholderNumber).css('height', '30px');
    },
    change: function(e, ui) {
        // When placeholder changes, animage away old one, and animate in new one, but only if a little delay has passed to avoid crazy jank town.
        if ($(ui.item).parent().hasClass('delayPlaceholderMovement') == false) {
            // If the parent doesn't have the 'delay' class, proceed to animating away the current placeholder.
            $('.temp-spacer-' + placeholderNumber).animate({
                height: "0px"
            }, 200, function() {
                $(this).remove();
            });
            // iterate the placeholder number to keep old and new ones separated.
            placeholderNumber = placeholderNumber + 1;

            // add and animate in the new location placeholder.
            $(ui.placeholder).before('<div style="height:0px;" class="temp-spacer-' + placeholderNumber + '"></div>');
            $('.temp-spacer-' + placeholderNumber).animate({
                height: "30px"
            }, 200);
        };
        // add the 'delay' class to the parent
        $(ui.item).parent().addClass('delayPlaceholderMovement');
        // and set a timeout to remove the parent after 40ms
        window.setTimeout(function() {
            $(ui.item).parent().removeClass('delayPlaceholderMovement');
        }, 40);
    },
    stop: function(event, ui) {
        // remove our fake placeholder and strip the regular placeholders negative margin.
        $('.temp-spacer-' + placeholderNumber).css('height', 0).remove();
        $(ui.placeholder).css('margin-top', '0px');
        // clear placeholder number so this doesn't go a bagillions after performing a few dragg events.
        placeholderNumber = 0;
    }
});


// CSS:
// Setting the height of the default placeholder. If you want to style the placeholder, you'd probably set an additional class on our replacement animated placeholder.
.tree-drop-placeholder {
    height: 30px;
    width: 100%;
}
Run Code Online (Sandbox Code Playgroud)

因此,默认占位符被删除并在 jquery UI 中非常突然地添加,它只是从一个位置将其添加到新位置,而无法添加 css 动画或任何内容。

我们在这里所做的是将默认占位符替换为我们自己的可以设置动画的占位符。我们为创建的每个替换占位符迭代一个唯一的编号,以便我们可以同时拥有多个现有的占位符,并以动画方式更逐渐地进出它们。

希望这可以帮助!尚未在许多浏览器中测试它,可能有比全局范围更好的地方来放置“placeholderNumber”变量。