开发者

jQuery fade in/out toggle method

开发者 https://www.devze.com 2023-04-10 17:09 出处:网络
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 fad开发者_StackOverflowe out. I am

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 fad开发者_StackOverflowe 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>


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

then you can simply do this:

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


Why don't you use fadeToggle()?

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号