3

I have three different buttons with three different divs that each toggle an applicable div in same place. Trying to do 2 things.

  1. Add class on .app-icon button when relative .app-div is showing so that I can add background color when active.
  2. 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:

<a href="#" class="app-icon"><i class="fa fa-calculator" aria-hidden="true"></i></a>
<a href="#" class="app-icon"><i class="fa fa-sun-o" aria-hidden="true"></i></a>
<a href="#" class="app-icon"><i class="fa fa-rss" aria-hidden="true"></i></a>

<div class="app-div">
content div 1
</div>

<div class="app-div">
content div 2
</div>

<div class="app-div">
content div 3
</div>

jQuery:

$('.app-icon').click(function() {
  var index = $(this).index();
  $('.app-div').eq(index).toggle().siblings('.app-div').hide();
});

//animate.css effect
$('.app-div').addClass('animated fadeInUp');

Here's what I have so far: https://jsfiddle.net/frshjb373/a5osx7y6/3/

4 Answers 4

2

One option is to handle an if statement checking if the icon clicked is actually active like this:

$('.app-icon').click(function() {
  var index = $(this).index();
  if($(this).hasClass('active')) {
     $(this).removeClass('active');
     $('.app-div').eq(index).addClass('fadeOutDown')
  } else {
     $('.app-icon').removeClass('active');
     $(this).addClass('active')
     $('.app-div').hide().eq(index).show().removeClass('fadeOutDown')
  }
});

Updated Demo

Sign up to request clarification or add additional context in comments.

1 Comment

exactly what I was looking for! Works great.
2

Not sure if that's what you're looking for (it's difficult to make "no-glitchy" toggle using only animate.css)

$('.app-icon').click(function() {
  $('.app-icon.active').not(this).removeClass('active');
  var index = $(this).toggleClass('active').index();
  $('.app-div').eq(index).toggleClass('fadeInUp fadeOutDown').show().siblings('.app-div').removeClass('fadeInUp').addClass('fadeOutDown').hide();
});

and apply style for .active class

.app-icon.active{ color:green; }

JSFiddle

Comments

1
  1. "addClass" and "removeClass" (two last sentences)
  2. I think the ".hide()" was the problem, but you also need to add and remove class for "fadeOutDown" effect.

Try this:

$('.app-icon').click(function() {
  var index = $(this).index();
  if($('.app-div').eq(index).is(":visible"))
  {
    $('.app-div').eq(index).addClass('fadeOutDown').slideUp();
  }
  else
  {
    $('.app-div').eq(index).removeClass('fadeOutDown').slideDown().siblings('.app-div').slideUp();
  }
  $('.app-icon.current').removeClass("current");
  $(this).addClass("current");
});

EDIT: To remove the current class by clicking itself you should move the addClass("current") inside the else statement. Here is a working demo https://jsfiddle.net/a5osx7y6/5/

Comments

1

Here is the perfect finishing to Artur reply

HTML

<a href="#" class="app-icon"><i class="fa fa-calculator" aria-hidden="true"></i></a>
<a href="#" class="app-icon"><i class="fa fa-sun-o" aria-hidden="true"></i></a>
<a href="#" class="app-icon"><i class="fa fa-rss" aria-hidden="true"></i></a>

<div style="position: relative">
    <div class="app-div">
    content div 1
    </div>

    <div class="app-div">
    content div 2
    </div>

    <div class="app-div">
    content div 3
    </div>
</div>

CSS

.app-div {display: none; position: absolute; top: 0; left: 0;}
.app-icon .fa {transition: background-color 0.5s ease;}
.app-icon .fa:hover {background: #ccc;}
.app-icon.active {background: #CD87BA;}

JavaScript

$('.app-icon').click(function() {
  $('.app-icon.active').not(this).removeClass('active');
  var index = $(this).toggleClass('active').index();
  $('.app-div').eq(index).toggleClass('fadeInUp fadeOutDown').show().siblings('.app-div').removeClass('fadeInUp').addClass('fadeOutDown');
});

//animate.css effect
$('.app-div').addClass('animated fadeOutDown');

Working JSFiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.