Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fadeIn / fadeOut based on a boolean

I was wondering if I really have to write:

if (status) {
    $('#status-image-' + id).fadeIn();
} else {
    $('#status-image-' + id).fadeOut();
}

or there is a function to which I can provide my boolean status, something like:

$('#status-image-' + id).fade(status);

I've seen fadeToggle, but it doesn't accept a boolean status parameter.

like image 520
stivlo Avatar asked Nov 11 '11 08:11

stivlo


People also ask

What is fadeIn and fadeOut in jQuery?

The jQuery fadeToggle() method toggles between the fadeIn() and fadeOut() methods. If the elements are faded out, fadeToggle() will fade them in. If the elements are faded in, fadeToggle() will fade them out. Syntax: $(selector).fadeToggle(speed,callback);

What is fadeOut in Javascript?

Definition and Usage. The fadeOut() method gradually changes the opacity, for selected elements, from visible to hidden (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 fadeIn() method.

How does jQuery fadeOut work?

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.


1 Answers

No, there is not, but you can make one like that:

jQuery.fn.fadeInOrOut = function(status){
    return status ? this.fadeIn() : this.fadeOut();
}

and then call it like that (see this jsfiddle for a proof):

$('#status-image-' + id).fadeInOrOut(status);

Is it what you wanted?

like image 160
Tadeck Avatar answered Oct 04 '22 21:10

Tadeck