I have three different buttons with three different divs that each toggle an applicable div in same place. Trying to do 2 things.
- Add class on
.app-icon
button when relative.app-div
is showing so that I can add background color when active. - Add an animate.css effect when toggling same
.app-div
open and closed so that when the div disappears it slides down, rather than just abruptly disappearing.
HTML:
JavaScript
x
16
16
1
<a href="#" class="app-icon"><i class="fa fa-calculator" aria-hidden="true"></i></a>
2
<a href="#" class="app-icon"><i class="fa fa-sun-o" aria-hidden="true"></i></a>
3
<a href="#" class="app-icon"><i class="fa fa-rss" aria-hidden="true"></i></a>
4
5
<div class="app-div">
6
content div 1
7
</div>
8
9
<div class="app-div">
10
content div 2
11
</div>
12
13
<div class="app-div">
14
content div 3
15
</div>
16
jQuery:
JavaScript
1
8
1
$('.app-icon').click(function() {
2
var index = $(this).index();
3
$('.app-div').eq(index).toggle().siblings('.app-div').hide();
4
});
5
6
//animate.css effect
7
$('.app-div').addClass('animated fadeInUp');
8
Here’s what I have so far: https://jsfiddle.net/frshjb373/a5osx7y6/3/
Advertisement
Answer
One option is to handle an if
statement checking if the icon clicked is actually active like this:
JavaScript
1
12
12
1
$('.app-icon').click(function() {
2
var index = $(this).index();
3
if($(this).hasClass('active')) {
4
$(this).removeClass('active');
5
$('.app-div').eq(index).addClass('fadeOutDown')
6
} else {
7
$('.app-icon').removeClass('active');
8
$(this).addClass('active')
9
$('.app-div').hide().eq(index).show().removeClass('fadeOutDown')
10
}
11
});
12