Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if element was faded out

I want to have two buttons, one to fade div1 out, and one either to fade div1 in, or to fade out the button itself, if the div1 is already hidden. here's the code, pretty unnecessary though, cause my main problem is "if" statement...

$('#b > button').click(function(){
    $('#div1').fadeOut(400)
});

$('#div2 > button').click(function(){
    $('#div1').fadeIn(400)
});
like image 801
Leo Avatar asked Dec 28 '12 21:12

Leo


People also ask

What does fade toggle() method do?

Definition and Usage. The 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.

Which function is used to give a fading effect to an element?

The jQuery fadeIn() method is used to fade in a hidden element. Syntax: $(selector).fadeIn(speed,callback);

How to set fadeOut time in jQuery?

jQuery fadeOut() Effect Example with speed parameter ready(function(){ $("button"). click(function(){ $("p"). fadeOut(2500); }); }); Or you can specify the speed as “slow” or “fast” as shown below.

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.


2 Answers

FadeOut simply changes the display to none.

Check if the display is none using jQuery's $('selector').css('display') or $('selector').is(':visible')

like image 80
Mark Meyer Avatar answered Sep 19 '22 13:09

Mark Meyer


Why not to disable/enable the buttons?

$('#b > button').click(function(){
    $('#div1').fadeOut(400, function() {
        $(this).prop('disabled', true);
        $('#div2 > button').prop('disabled', false);
    });
});

$('#div2 > button').click(function(){
    $('#div1').fadeIn(400, function() {
        $(this).prop('disabled', true);
        $('#b > button').prop('disabled', false);
    });
});
like image 29
AlecTMH Avatar answered Sep 23 '22 13:09

AlecTMH