jQuery add/remove Class with fadeIn/Out
Asked Answered
S

6

24

I would to apply a fadeIn effect to a addClass function..and fadeOut to removeClass...

Can you help me?

This is my code

$('#loader'+idTurno).addClass('loader');

...

$('#loader'+idTurno).removeClass('loader');
Stemma answered 31/3, 2013 at 16:48 Comment(0)
D
48

Fade In:

$("#loader").fadeIn("slow", function() {
    $(this).addClass("loader");
});

Fade Out:

$("#loader").fadeOut("slow", function() {
    $(this).removeClass("loader");
});

As another user said, you may want to look into using toggleClass.

Derogative answered 31/3, 2013 at 16:49 Comment(8)
@minitech I tried the above solution but when the second function runs(fadeout) the element completely disappears.In my case the elements is dropdown menu(select).What I've noticed is that to the element that "disappears" from view a style of display:none is applied. Why that happens?Sideboard
@DimitrisPapageorgiou Read the documentation of fadeOut(). It talks about how after the opacity is set to 0, the display property is set to display: none - api.jquery.com/fadeoutDerogative
@minitexh yes....you are right,what are my alternatives?Anyway,the fiddle is here jsfiddle.net/fiddlehunt/achgnmcv try chaning a value to the From menuSideboard
@DimitrisPapageorgiou First, you're responding to the wrong person ;) Second, please open up another question or do some more in-depth searching.Derogative
Unfortunately I am banned from making new question.I 'll see what I am going to do.Anyway,thanks.Sideboard
not a solution! hides the item, not just removing classVehicle
You can use this option, but just add another div to overlay on top of #loader that contains the css styling you want. Fade that div in and out which will create the effect you want.Chuu
Comments say it all. Please note the answer below that is under water by -9 or so. That is the answer that solved my problem instantly.Admixture
S
18

Another way to achieve that, using your original jQuery code, the CSS way :

#loader {
  transition: opacity 500 ease-in-out;
}

Smoother animation, easier to maintain.

Schoenfelder answered 28/2, 2014 at 14:35 Comment(2)
Does nothing for me.Semidiurnal
You have to use an additional class, like the asker posted: .loader which will set the opacity to 0 or 1. Then toggle this class with JS.Schoenfelder
K
5
#loader {
  transition: all 0.9s ease-out 0s;
}
Kareem answered 1/9, 2017 at 5:40 Comment(0)
W
2

Perhaps used setTimeout function after fadeIn class

 $('#loader').addClass('loader').fadeIn('slow');
 setTimeout(function(){  $('#loader').removeClass('loader'); }, 1000);
Wicket answered 4/8, 2020 at 18:7 Comment(1)
This worked nicely for me. I tried adding .fadeOut to .removeClass but, as noted above, at the end of the fade it sets the selector to display none so I just have it fading in and the being removed.Volcanism
O
0

Make it simple :

$('#loader'+idTurno).addClass('loader').fadeIn(1000);
$('#loader'+idTurno).removeClass('loader').fadeIn(1000);
Outride answered 16/9, 2016 at 11:50 Comment(1)
This makes the entire DIV fadein or fadeout, not just the class.Schoolgirl
A
-7

You should add a duration to the remove/addClass method:

$('#loader'+idTurno).addClass('loader',500);
$('#loader'+idTurno).removeClass('loader',500);
Aletaaletha answered 28/2, 2014 at 13:44 Comment(4)
This answer is blatantly wrong, as seen at api.jquery.com/addClass there is no duration property for the $.addClass() or $.removeClass() methods.Globe
If we are using only jquery, then your statement would be correct @DavidScherer. If however, the OP uses jqueryUI the above would be correct as jqueryUI (while quite hefty to add such a small feature) does indeed support fading a class in and out with a duration. api.jqueryui.com/addClassIntramundane
+1 This is the best, simplest answer. It not only works perfectly, it even has an "easing" parameter for a linear or swing transition. Sad that the answer is below water. @DennisBartlettAdmixture
9 years is not too long for retribution I suppose... @AdmixtureAletaaletha

© 2022 - 2024 — McMap. All rights reserved.