简体   繁体   English

一键式如何从单个元素添加和删除类

[英]How to add and remove a class from a single element with one click…

I'm trying to trigger a complete CSS3 animation with a single click on an anchor element. 我试图通过单击锚元素来触发完整的CSS3动画。 My current version is working (based on lots of searching here and on Google), but it requires two clickable elements to be present. 我当前的版本正在运行(基于此处和Google上的大量搜索),但是它需要两个可点击的元素。

Here's what I have so far: 这是我到目前为止的内容:

http://codepen.io/ProfessorSamoff/pen/rVNOdv http://codepen.io/ProfessorSamoff/pen/rVNOdv

As you'll see, the jQuery is pretty typical: 如您所见,jQuery非常典型:

$('a.puff').click(function() {
    $('a.puff').addClass('active');
    $(this).removeClass('active');
});

I know that I'm probably missing something pretty simple, so any help is appreciated. 我知道我可能缺少一些非常简单的东西,因此可以提供任何帮助。

Thanks, Tim 谢谢,蒂姆

CSS alone doesn't provide a mechanism to act on a keyframe animation/transition end, however it does fire a javascript-detectible event, as explained here . CSS本身并不提供一种机制来作用于关键帧动画/过渡结束,但它确实火了JavaScript的检测的事件,如解释在这里

Your repeatable puff animation will look like this : 您的重复吹动画将如下所示:

$('a.puff').each(function() {
    $(this).attr('data-puff', $(this).text());
}).on('click', function() {
    var $a = $(this).addClass('active').one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function() {
        $a.removeClass('active');
    });
});

As far as I'm aware this is as simple as you can make it. 据我所知,这很简单。

Updated codepen 更新的代码笔

$(document).ready(function() {
   $('.puff').each(function() {
      $(this).attr('data-puff', $(this).text());
   });
   $('a.puff').click(function() {
      var $this = $(this);
      $this.addClass('active');
      setTimeout(function(){ $this.removeClass('active'); }, 150);
   });
});

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

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