简体   繁体   English

使用Jquery和'.css'的CSS动画

[英]CSS Animation using Jquery and '.css'

So I have a piece of code. 所以我有一段代码。 The purpose is to play a selected animation from Animate.css on click . 目的是单击时播放Animate.css中的选定动画。

The code 编码

  $(".container>parent").click(function () {
    $('.element').css({
      'animation': 'fadeInUp .2s',
      '-webkit-animation': 'fadeInUp .2s'
    });
  });

The problem 问题

Animation runs, but only one time. 动画只能运行一次。 'Infinite' attribute causes chaos :P 'Infinite'属性导致混乱:P

What else could I do, to play that animation every single time someone click it? 我还能做些什么,以便每当有人单击该动画时播放该动画?

Thanks for your time. 谢谢你的时间。

My HTML : 我的HTML

            <span class="parent">
                <img src="assets/myimage.png" class="filter-image">
                <span class="element">Text</span>
            </span>  

I want to animate the text everytime I click it. 我想在每次单击文本时为其设置动画。

$(".container>parent").click(function() {
    $('.element').css({
        'animation': 'fadeInUp .2s',
        '-webkit-animation': 'fadeInUp .2s'
    });

    setTimeout(function(){
        $('.element').removeAttr('style');
    },300);
});

The animation won't work the second time if you don't remove animation class after the current animation finishes. 如果您在当前动画完成后不删除动画类,则该动画将第二次不起作用。

But how to remove animation property after the animation finishes? 但是,动画结束后如何删除动画属性?

Here's a snippet: 这是一个片段:

var support = {};
support.animation = (function() {
    var animationEnd = (function() {
        var element = document.body || document.documentElement,
            animEndEventNames = {
                WebkitAnimation : 'webkitAnimationEnd',
                MozAnimation    : 'animationend',
                OAnimation      : 'oAnimationEnd oanimationend',
                animation       : 'animationend'
            }, name;

        for (name in animEndEventNames) {
            if (element.style[name] !== undefined) return animEndEventNames[name];
        }
    }());

    return animationEnd ? { end: animationEnd } : false;
})();



function animate(elem, cls, callback) {
    var $elem = $(elem);

    var onEndCallbackFn = function(ev) {
        if (support.animation) {
            $elem.removeClass(cls);
            this.removeEventListener(support.animation.end, onEndCallbackFn);
        }

        if (callback && typeof callback === 'function') { callback.call(this, ev); }
    };

    if (support.animation) {
        $elem.addClass(cls);
        $elem[0].addEventListener(support.animation.end, onEndCallbackFn);
    } else {
        onEndCallbackFn();
    }
}

usage is simple, just call animate function, like this: 用法很简单,只需调用animate函数,如下所示:

animate($('.selector'), 'classWithAnimation', callbackFn);

In you case, you said you are using animate.css library: 在这种情况下,您说您正在使用animate.css库:

$(".container>parent").click(function() {
    animate($('.element'), 'animated fadeInUp', function() {
        console.log('animation complete');
    );
});

Live example: jsFiddle 实时示例: jsFiddle

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

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