Woolf Faulkner
Woolf Faulkner

Reputation: 108

jQuery fade in/out toggle method

I am having trouble with an if / else statement for fading in and out using a toggle link. I can get the <div> to fade in, however, I can not seem to get it to fade out. I am somewhat new to if and else statements.

You can see a live demo here.

Here is the script in question:

<script type="text/javascript">


    function toggleFader() {
    if ($("#fade_content").is(":hidden")) {
        $("#contentThatFades").animate(
            {
                opacity: "0"    
            },
            600,
            function(){
                $("#fade_content").fadeOut();
            }
        );
    }
    else {
        $("#fade_content").fadeIn(600, function(){
            $("#contentThatFades").animate(
                {
                    opacity: "1"
                },
                600
            );
        });
    }
}


</script>

Upvotes: 1

Views: 11043

Answers (2)

ronakg
ronakg

Reputation: 4212

Why don't you use fadeToggle()?

function toggleFader() {
    $("#fade_content").fadeToggle(600);
}

Upvotes: 2

rgin
rgin

Reputation: 2311

jQuery already has a function for this. See http://api.jquery.com/fadeToggle/

then you can simply do this:

$("#fade_content").fadeToggle(600);

Upvotes: 16

Related Questions