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);
}
精彩评论