javascript - Toggle multiple divs with multiple buttons using animate.css - jQuery - Stack Overflow

I have three different buttons with three different divs that each toggle an applicable div in same pla

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: /

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/frshjb373/a5osx7y6/3/

Share Improve this question asked Sep 22, 2016 at 15:54 frshjb373frshjb373 6279 silver badges27 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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

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

  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/a5osx7y6/5/

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744962462a4603472.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信