开发者

Slide subsequent elements as well when using jQuery Ui's slide effect

开发者 https://www.devze.com 2023-03-31 14:24 出处:网络
I\'m looking for a way to toggle an eleme开发者_开发百科nt\'s visibility by sliding it in while smoothly pushing away those elements that are in the target position.

I'm looking for a way to toggle an eleme开发者_开发百科nt'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!


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.


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


You could add a containing DIV and set its height :

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号