简体   繁体   English

CSS动画使用Jquery添加类和删除类

[英]CSS animation add class and remove class using Jquery

I have one question about add and remove class using css animation. 我有一个关于使用CSS动画添加和删除类的问题。

I have created this DEMO from codepen.io 我已经从codepen.io创建了这个DEMO

In this demo you can see there is six round div. 在此演示中,您可以看到有六个回合div。 Also there is one link ( Click here ). 也有一个链接( 单击此处 )。

If you click to Click here button then you can see the CSS animation. 如果单击“单击此处”按钮,则可以看到CSS动画。 So i want to add a jquery code. 所以我想添加一个jQuery代码。 Like first the six round div is display:none; 像第一个一样,六个回合的div display:none; when you click Click here button then six round div open with animation but only one time. 当您单击“ 单击此处”按钮时,将以动画方式打开六个回合div,但只有一次。 after then when you click again Click here button then six round div automatically remove with css animation. 之后,当您再次单击时,请单击此处按钮,然后六个回合div会随着CSS动画自动删除。

Anyone can help me here ? 有人可以在这里帮助我吗?

First of all you need to do to delete the following line within the .circle 首先,您需要删除.circle的以下行

-webkit-animation-iteration-count: infinite;
-webkit-animation-direction:alternate;

Then you can use Mouser 's and sareed 's code: 然后,您可以使用Mousersareed的代码:

$(document).ready(function() {
     var circle = $('.circle');
     $(".note a").click(function(e) {
      e.preventDefault();
     $('.wrap').css('display', 'block');

        if (circle.hasClass('bounceInUp')) {
         circle.removeClass('bounceInUp').addClass('bounceOutDown');
          }
           else 
          {
         $('.circle').addClass('animated bounceOutDown');
         circle.removeClass('bounceOutDown').addClass('bounceInUp');
           }
      });
  });

I hope this will help you. 我希望这能帮到您。

$(document).ready(function() {
  $('.wrap').css('display', 'none'); 
  $(".note a").click(function(e) {
    e.preventDefault();

     $('.wrap').css('display', 'block'); 
     $('.circle').addClass('animated bounceInUp');
     $(this).parent('.note a').removeClass('.circle bounceInUp');
     $(".note a").off('click');   //remove click handler.      

     //Adding the new click handler
     $(".note a").click(function(e) {
        e.preventDefault();

        $('.circle').addClass('animated bounceOutDown');
        $(this).parent('.note a').removeClass('.circle animated bounceOutDown');
        $(".note a").off('click');  //remove click handler again.
     });
  });
});

Add a second click handler to the button 将第二个单击处理程序添加到按钮

Check to see if the added class is present and remove/do animation if it is. 检查是否存在添加的类,如果存在,则删除/执行动画。 Check out hasClass(); hasClass();

Edit: Add something similar to this in your handler: 编辑:在您的处理程序中添加与此类似的内容:

var circle = $('.circle');
if (circle.hasClass('bounceInUp')) {
  circle.removeClass('bounceInUp').addClass('bounceOutDown');
}
else {
$('.circle').addClass('animated bounceOutDown');
  circle.removeClass('bounceOutDown').addClass('bounceInUp');
}

codepen codepen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM