views:

8339

answers:

4

If an item is being deleted then I would like to fade it out and slide the other elements up to fill the empty space. Now, when I use fadeOut() the item doesn't have a height at the end which results in the other items jumping up (instead of sliding up nicely).

How can I slideUp() and element right after fadeOut()?

+1  A: 

Can't you chain it?

$('myelement').fadeOut().slideUp();

EDIT:

Maybe this will help instead?

Kezzer
No it fades to nothing and then does a hard move on the DOM element.
Nick Berardi
Ah I see, that is a pain.
Kezzer
No, it jumps up at the end of the fadeOut().
bart
Your link shows how to fadeOut and SlideUp at the same time. I would slideUp after fadeOut has ended.
bart
A: 

The fadeOut function takes a second optional argument of a callback function, so you should be able to do something like this:

$('elementAbove').fadeOut(500, function() {
    $('elementBelow').slideUp();
});

EDIT: forgot to add the speed of the fadeOut as the first parameter

Ian Oxley
It still jumps up :(
bart
What is the HTML and CSS that you are manipulating?
Ian Oxley
it doesn't work because you esentially did the same thing as chaining. you need to fade to 1% first and then roll up
Nick Berardi
I'd disagree that this is the same thing as chaining: with chaining you could end up with both animations firing simultaneously due to the way timeouts; using a callback for the slideUp() should in theory ensure that it fires only after fadeOut has finished executing.
Ian Oxley
@Nick Berardi: I see what you mean about fading to 1% - I've just tried a quick demo and using fadeTo(speed, 0.1, callback) seems to do the trick.
Ian Oxley
+11  A: 
jQuery.fn.fadeThenSlideToggle = function(speed, easing, callback) {
  if (this.is(":hidden")) {
    return this.slideDown(speed, easing).fadeTo(speed, 1, easing, callback);
  } else {
    return this.fadeTo(speed, 0, easing).slideUp(speed, easing, callback);
  }
};

I tested it on JQuery 1.3.2, and it does work.

Edit: This is the code I called it from. #slide-then-fade is the ID of a button element, article-text is a class on a div tag:

$(document).ready(function() {
  $('#slide-then-fade').click(function() {
    $('.article-text').fadeThenSlideToggle();
  });
});

Edit 2: Modified to use the built-in slideUp.

Edit 3: Rewritten as a toggle, and using fadeTo

R. Bemrose
I had to drop the 'easing' parameter in order to get the 'callback' to work.
bart
Ah, OK. As I recall, the slides and fades have different arguments for easing, so the easing argument was useless anyway.
R. Bemrose
+10  A: 

Sounds like it would be better to use the jQuery fadeTo command

 $(function() {

     $("#myButton").click(function() {
         $("#myDiv").fadeTo("slow", 0.01, function(){ //fade
             $(this).slideUp("slow", function() { //slide up
                 $(this).remove(); //then remove from the DOM
             });
         });

     });

});

Working Demo here.

By performing each command in the callback function of the preceding command, the command will not run until the previous one completes; a "jump" occurs when the element is removed from the DOM without waiting for the slideUp to complete.

Russ Cam
nice demo, wish more folks would do it.
redsquare
Wish stackoverflow would provide a sandbox
Nick Berardi
Thanks. I discovered jsbin here on stackoverflow and have been using it since to provide workable demos in answers - http://jsbin.com
Russ Cam