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

发布于 2024-12-02 06:14:27 字数 471 浏览 3 评论 0原文

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

到目前为止,我尝试了 jQueryjQuery Ui 的幻灯片效果,似乎我需要这两者的混合。

虽然jQuery的幻灯片拉伸了相关元素,但我宁愿希望它像在带有overflow:hidden的容器中一样滑入和滑出(例如jQuery Ui 确实)。不幸的是,jQuery Ui 在动画过程中不会重新定位相邻元素,而是在动画完成后让它们跳到原来的位置。

我创建了一个 Fiddle 来向您展示我的意思。我的问题是:有没有办法让 jQuery Ui 中的幻灯片没有后续元素跳到其位置,而是平滑地移动到那里?

非常感谢您的建议!

I'm looking for a way to toggle an element's visibility by sliding it in while smoothly pushing away those elements that are in the target position.

Until now I tried both slide effects from jQuery and jQuery Ui and it seems like I need a blend of those two.

Whereas jQuery's slide stretches the element in question I'd rather want it to slide in and out like in a container with overflow: hidden (like jQuery Ui does). Unfortunately, jQuery Ui does not reposition neighboring elements during the animation but makes them jump to their place once the animation is finished.

I have created a Fiddle to show you what I mean. My question is: is there a way to have the slide in jQuery Ui without the subsequent element jumping to its place but instead move there smoothly?

Thank you very much for your advice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬色的秋千 2024-12-09 06:14:27

我采纳了 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();
            }
        });

您可以在此 Fiddle 中查看结果。

I took ManseUKs idea and extended the regular slide effect of jQuery Ui in so far that the height of the automatically created wrapper div is adjusted during the slide animation.

$.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();
            }
        });

You can view the result in this Fiddle.

没有你我更好 2024-12-09 06:14:27

谢谢你,jnns。干得好!
这是您的解决方案,针对 jQuery UI 版本 1.9 进行了更新。

$.effects.effect.customSlide = function (options) {
    var el = $(this),
        props = ['position', 'top', 'bottom', 'left', 'right'];

    // Set options
    var mode = $.effects.setMode(el, options.mode || 'show'); // Set Mode
    var direction = 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 = options.distance || (ref == 'top' ? el.outerHeight(true) : el.outerWidth(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: options.duration,
        easing: options.easing
    });
    el.animate(animation, {
        queue: false,
        duration: options.duration,
        easing: options.easing,
        complete: function () {
            if (mode == 'hide') el.hide(); // Hide
            $.effects.restore(el, props);
            $.effects.removeWrapper(el); // Restore
            if (options.callback) options.callback.apply(this, arguments); // Callback
            el.dequeue();
        }
    });
};

Thank you, jnns. Great work!
Here is your solution, updated for jQuery UI version 1.9.

$.effects.effect.customSlide = function (options) {
    var el = $(this),
        props = ['position', 'top', 'bottom', 'left', 'right'];

    // Set options
    var mode = $.effects.setMode(el, options.mode || 'show'); // Set Mode
    var direction = 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 = options.distance || (ref == 'top' ? el.outerHeight(true) : el.outerWidth(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: options.duration,
        easing: options.easing
    });
    el.animate(animation, {
        queue: false,
        duration: options.duration,
        easing: options.easing,
        complete: function () {
            if (mode == 'hide') el.hide(); // Hide
            $.effects.restore(el, props);
            $.effects.removeWrapper(el); // Restore
            if (options.callback) options.callback.apply(this, arguments); // Callback
            el.dequeue();
        }
    });
};
血之狂魔 2024-12-09 06:14:27

您可以添加一个包含 DIV 并设置其高度:

http://jsfiddle.net/Q2dSZ/4/

You could add a containing DIV and set its height :

http://jsfiddle.net/Q2dSZ/4/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文