使用jQuery Ui的幻灯片效果时,也可以滑动后续元素

jnn*_*nns 3 jquery jquery-ui slide jquery-effects

我正在寻找一种方法来切换元素的可见性,方法是将其滑入,同时平滑地推开目标位置中的元素.

到目前为止,我尝试了jQueryjQuery Ui的两种幻灯片效果,看起来我需要混合使用这两种效果.

虽然jQuery的幻灯片延伸了有问题的元素,但我宁愿它像在容器中一样滑入和滑出overflow: hidden(就像jQuery Ui那样).不幸的是,jQuery Ui不会在动画期间重新定位相邻元素,但会在动画结束后跳转到它们的位置.

我创造了一个小提琴来告诉你我的意思.我的问题是:有没有办法让jQuery Ui中的幻灯片没有后续元素跳到它的位置,而是顺利地移动到那里?

非常感谢您的建议!

jnn*_*nns 6

我采用了ManseUK的想法并扩展了jQuery Ui的常规幻灯片效果,以至于在幻灯片动画期间调整了自动创建的包装div的高度.

$.effects.customSlide = function(o) {

    return this.queue(function() {

        // Create element
        var el = $(this),
            props = ['position', 'top', 'bottom', 'left', 'right'];

        // Set options
        var mode = $.effects.setMode(el, o.options.mode || 'show'); // Set Mode
        var direction = o.options.direction || 'left'; // Default Direction
        // Adjust
        $.effects.save(el, props);
        el.show(); // Save & Show
        $.effects.createWrapper(el).css({
            overflow: 'hidden'
        }); // Create Wrapper
        var ref = (direction == 'up' || direction == 'down') ? 'top' : 'left';
        var motion = (direction == 'up' || direction == 'left') ? 'pos' : 'neg';
        var distance = o.options.distance || (ref == 'top' ? el.outerHeight({
            margin: true
        }) : el.outerWidth({
            margin: true
        }));
        if (mode == 'show') el.parent().css('height', 0);

        if (mode == 'show') el.css(ref, motion == 'pos' ? (isNaN(distance) ? "-" + distance : -distance) : distance); // Shift
        // Animation
        var animation = {};
        animation[ref] = (mode == 'show' ? (motion == 'pos' ? '+=' : '-=') : (motion == 'pos' ? '-=' : '+=')) + distance;

        el.parent().animate({
            height: (mode == 'show' ? distance : 0)
        }, {
            queue: false,
            duration: o.duration,
            easing: o.options.easing
        });
        el.animate(animation, {
            queue: false,
            duration: o.duration,
            easing: o.options.easing,
            complete: function() {
                if (mode == 'hide') el.hide(); // Hide
                $.effects.restore(el, props);
                $.effects.removeWrapper(el); // Restore
                if (o.callback) o.callback.apply(this, arguments); // Callback
                el.dequeue();
            }
        });
Run Code Online (Sandbox Code Playgroud)

您可以在此小提琴中查看结果.