What I'm trying to do is IF I click on a span
No. What you're trying to do is "when you click on a span..."
then some elements in a DIV get faded out
...a div fades out.
and ELSE you click again on that span
Have you ever heard anyone say "and else you click again"? If it's not how you speak, you shouldn't write it either.
What you're trying to describe is what happens when you click on the span a second time. It's actually not important how many times the span has been clicked on, what's important is the state of the div. What you're describing goes back to what happens when the span is clicked.
those elements would get faded in.
So what you've tried to say so far is:
When you click on a span, check whether a div is visible; if it is, fade out, else, fade in.
There I'm also using css() to change some styles of the elements remained in that DIV.
It sounds like what you're trying to say is that you're also styling the div using jQuery's css method. Given the state of the code this appears to (mostly) be the case.
Don't use the css method.
It's useful to use jQuery's css method when you absolutely require dynamic styles. Most of the time, most people don't need dynamic styles. Most of the time, most people need dynamic sets of styles. These are best represented as classes that should be left in stylesheets. HTML belongs in .html files, JS belongs in .js files, CSS belongs in .css files.
So what you really want is to change a class (or multiple) when the div has finished animating.
All of that without even writing any code. To tie it all together, here's generally what you'd want to do:
$('.some-span').on('click', function () {
//when you click on a span
var divIsVisible;
//check whether a div is visible
divIsVisible = $('.some-div').is(':visible');
if (divIsVisible) {
//if it is, fade out
$('.some-div').fadeOut(function () {
//when it's done fading out, add some classes
$(this).addClass('is-hidden');
//add other classes
});
} else {
//else fade in
$('.some-div').fadeIn(function () {
//when it's done fading in, remove some classes
$(this).removeClass('is-hidden');
//remove other classes
});
}
});
if.. elseworks.css({'width':'45px', 'height': '200px', 'overflow': 'hidden'}), etc but again not sure what your end goal is...if..elseis a synchronous conditional statement, what you're trying to describe is an asynchronous animation. While you'd useif..elsestatements in the asynchronous callback, the way you've phrased what you're after is incongruous with the meaning of the words you've used in the context of JavaScript programming.