3

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!

jnns
  • 5,148
  • 4
  • 47
  • 74

3 Answers3

6

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.

Community
  • 1
  • 1
jnns
  • 5,148
  • 4
  • 47
  • 74
1

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();
        }
    });
};
Community
  • 1
  • 1
Carson Herrick
  • 357
  • 4
  • 13
0

You could add a containing DIV and set its height :

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

Manse
  • 37,765
  • 10
  • 83
  • 108
  • Sorry for not being sufficiently clear on what I really want to achieve. Subsequent `div`s should move, but not instantaneous. – jnns Aug 30 '11 at 14:25
  • So you want the first 4 divs to collapse and then the other div to slide into the empty space ? – Manse Aug 30 '11 at 14:29
  • That's exactly what I want, yes. – jnns Aug 30 '11 at 14:46
  • I think the method is along the same lines - create a container div with positioning relative - make the bottom div (div2) positioned absolute (bottom : 0) then slide the tops divs up then animate the bottom div up .... i cannot get onto jsFiddle at the moment - its very very very slow ! – Manse Aug 30 '11 at 15:09