Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: FadeOut then SlideUp

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()?

like image 596
bart Avatar asked Apr 09 '09 14:04

bart


People also ask

How to stop fadeOut jQuery?

mouseover( function () { $(this). finish(). fadeOut(); } ); finish(): Stops the currently-running animation, remove all queued animations, and complete all animations for the matched elements.

How to set fadeOut time in jQuery?

The jQuery fadeOut() method is used to fade out a visible element. Syntax: $(selector). fadeOut(speed,callback);

How does jQuery fadeOut work?

The . fadeOut() method animates the opacity of the matched elements. Once the opacity reaches 0, the display style property is set to none , so the element no longer affects the layout of the page. Durations are given in milliseconds; higher values indicate slower animations, not faster ones.

What is fadeIn and fadeOut in jQuery?

jQuery fadeIn() Method The fadeIn() method gradually changes the opacity, for selected elements, from hidden to visible (fading effect). Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: This method is often used together with the fadeOut() method.


5 Answers

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

 $(function() {       $("#myButton").click(function() {          $("#myDiv").fadeTo("slow", 0.00, 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.

like image 58
Russ Cam Avatar answered Oct 02 '22 11:10

Russ Cam


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

like image 43
Powerlord Avatar answered Oct 02 '22 10:10

Powerlord


Can't you chain it?

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

EDIT:

Maybe this will help instead?

like image 27
Kezzer Avatar answered Oct 02 '22 12:10

Kezzer


$("#id").fadeIn(500, function () {

    $("#id2").slideUp(500).delay(800).fadeOut(400);

});
like image 43
albert Avatar answered Oct 02 '22 12:10

albert


Try $('.row').animate({ height: 'toggle', opacity: 'toggle' }, 'slow').slideUp();

demo Here

like image 31
Nithee Avatar answered Oct 02 '22 12:10

Nithee