欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

jQuery fade in/out toggle method

shiping1 的头像

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 elsestatements.

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>
shareimprove this question
 

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

then you can simply do this:

$("#fade_content").fadeToggle(600);
shareimprove this answer
 
   
Thanks for responding. I soon found this out. thanks again! –  Woolf Faulkner Jan 29 '12 at 4:52

Why don't you use fadeToggle()?

function toggleFader() {
    $("#fade_content").fadeToggle(600);
}
shareimprove this answer
 
   
Thanks for responding. I soon found this out. thanks again! –  Woolf Faulkner Jan 29 '12 at 4:53
普通分类: