combining multiple css animations into one overall animation

前端 未结 2 1856
旧巷少年郎
旧巷少年郎 2021-01-31 08:20

I have a set of animations that I queue up one after the other to create a bigger overall animation. For the sake of simplicity, I\'ve created a simple fiddle to demo what I mea

相关标签:
2条回答
  • 2021-01-31 08:54

    One animation one element is how it works as the animations change the styles of a single element. You can however apply delays to the animations to achieve what you want, allowing you to move pretty much everything out of JS.

    This example merges your .outside an .inside animations, by basically appending them with a comma to the rule and you JS now just adds the class like this -webkit-animation-name: button-bounce, rotate, skyblue;

    jsFiddle

    CSS

    .outside.animate {
        -webkit-animation-delay: 0s, .5s, .5s;
        -webkit-animation-duration: 500ms, 1000ms, 1000ms;
        -webkit-animation-name: button-bounce, rotate, skyblue;
    }
    
    .animate {
        -webkit-animation-fill-mode: forwards;
        -webkit-animation-timing-function: ease;
        -webkit-transform: translateZ(0);
    }
    
    .outside.animate .inside {
        -webkit-animation-delay: .5s, .5s, 1.5s;
        -webkit-animation-duration: 1000ms, 1000ms, 750ms;
        -webkit-animation-name: rotate, magenta, scale-in;
    }
    

    New animations

    @-webkit-keyframes magenta {
        0% { border: 1px solid magenta; }
        99.99% { border: 1px solid magenta; }
        100% { border: 1px solid skyblue; }
    }
    @-webkit-keyframes skyblue {
        0% { border: 1px solid skyblue; }
        99.99% { border: 1px solid skyblue; }
        100% { border: 1px solid magenta; }
    }
    

    JavaScript

    $(document).ready(function() {
        $(document).click(function() {
            var count = 0;
            var jqElement = $('.outside');
            if (!jqElement.hasClass('animate')) {
                jqElement.addClass('animate');
                jqElement.on('animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd', function(event) {
                    count++;
                    if (count >= 6) {
                        jqElement.off('animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd');
                        jqElement.removeClass('animate');
                    }
                });
            }
        });
    });
    
    0 讨论(0)
  • 2021-01-31 09:15

    You can use multiple animations separate by comma in shorthand property:

    .selector
    {
        animation: animation-name 2s infinite,
        other-animation-name 1s;
    }
    
    0 讨论(0)
提交回复
热议问题