Home > Web Front-end > CSS Tutorial > Beginner's article: How to use CSS to implement simple skeletal animation (code sharing)

Beginner's article: How to use CSS to implement simple skeletal animation (code sharing)

奋力向前
Release: 2021-09-18 14:05:21
forward
2478 people have browsed it

In the previous article "Teach you how to use shell scripts to implement quick server settings (with code)", I introduced how to use shell scripts to implement quick server settings. The following article will introduce to you how to use CSS to implement simple skeletal animation. Let's take a look at how to do it.

Beginner's article: How to use CSS to implement simple skeletal animation (code sharing)

#1. Background

One day the designer came to me and said, “This wish sign doesn’t look good hanging there stupidly. It’s an animation effect, just swing it left and right, it’s very simple!” I thought, OK, let’s improve the user’s visual experience, let’s do it.

Beginners article: How to use CSS to implement simple skeletal animation (code sharing)

Ten minutes later, no, the left and right swing was so fake, it didn’t look like the real wind blowing effect.

Note: The speed of the animation and the amplitude of the swing are accelerated here.

.animate-1 {
    animation: swing1 1s ease-in-out infinite;
    transform: rotate(-5deg);
    transform-origin: top center;
}
@keyframes swing1 {
    0% { transform: rotate(-5deg); }
    50% { transform: rotate(5deg);}
    100% { transform: rotate(-5deg);}
}
Copy after login

Beginners article: How to use CSS to implement simple skeletal animation (code sharing)

Think calmly, why this swing has no soul. So I picked up the work card and started swinging it to see what the swing effect was like in reality. Finally, it suddenly dawned on me: It turns out that the wish card in reality (the same as the work card) does not swing as a whole when it is stressed. It will be divided into several parts and swing in association according to the node position. This is actually a simple skeletal animation! So how to achieve it?

2. Skeleton Animation

Here we will take the wish card swing animation as an example, and we will study together how to use css to achieve it.

2.1 Separate elements

To make animated elements move separately, you first need to split the elements. The basis for splitting is the nodes mentioned above, which are the so-called joints in skeletal animation. For example, this wish card has two joints, one on the top and one below the card, so we can split it into 3 animation elements:

Beginners article: How to use CSS to implement simple skeletal animation (code sharing)

2.2 Splicing elements

<div>
    <!--元素1-->
    <div class="item-1"></div>
    <!--元素2-->
    <div class="item-2"></div>
    <!--元素3-->
    <div class="item-3"></div>
</div>
Copy after login

This seems simple, but if you don’t understand skeletal animation, you will fall into a pit. The above is a wrong demonstration. In order to deepen everyone's understanding, I dug a hole specially

2.3 Add animation

Based on the above, we can Part of it adds swing animations of different amplitudes and directions.

<div class="animate-2">
    <!--元素1-->
    <div class="item-1"></div>
    <!--元素2-->
    <div class="item-2"></div>
    <!--元素3-->
    <div class="item-3"></div>
</div>
Copy after login
.animate-2 .item-1 {
    /* 设置margin是为了定位,使其部分重叠在一起 */
    margin-bottom: -8px;
    margin-left: 18px;
    position: relative;
    z-index: 1;
    animation: swing2-1 1s ease-in-out infinite;
    transform: rotate(-3deg);
    transform-origin: top center;
}
.animate-2 .item-2 {
    animation: swing2-2 1s ease-in-out infinite;
    transform: rotate(5deg);
    transform-origin: top center;
}
.animate-2 .item-3 {
    margin-top: -5px;
    margin-left: 17.5px;
    position: relative;
    animation: swing2-3 1s ease-in-out infinite;
    transform: rotate(-5deg);
    transform-origin: top center;
}
@keyframes swing2-1 {
    0% { transform: rotate(-3deg); }
    50% { transform: rotate(3deg);}
    100% { transform: rotate(-3deg);}
}
@keyframes swing2-2 {
    0% { transform: rotate(5deg); }
    50% { transform: rotate(-5deg);}
    100% { transform: rotate(5deg);}
}
@keyframes swing2-3 {
    0% { transform: rotate(-5deg); }
    50% { transform: rotate(5deg);}
    100% { transform: rotate(-5deg);}
}
Copy after login

Done? Let’s take a look at the effect

Beginners article: How to use CSS to implement simple skeletal animation (code sharing)

#Oh my God, what is this! ! ! It does look like the swing is more realistic than the overall swing, with different elements having different swing amplitudes and directions. But it's misplaced.

Continuing to think calmly, the problem lies in that each sub-animation of skeletal animation is related, and each animation we designed above is independent. For example, when the red rope at the top swings, it will pull the sign below, causing the position of the sign below to change. The sign below plays its own swinging animation while changing its position. This is skeletal animation!

2.4 Fill in the pit - realize skeletal animation from js to understand its principle

The source code is here, because it is on YouTube, in order to avoid some students who are not able to surf the Internet scientifically. Yes, so take the following running action as an example to explain the js implementation process

https://github.com/bit101/CodingMath/tree/master/episode44

  • According to the initial state of the thigh and the current rotation speed, calculate the position of the thigh in the next frame;

  • Calculate the calf based on the current position of the thigh and the current speed of the calf The position of the next frame;

  • ...Infinite loop...

Beginners article: How to use CSS to implement simple skeletal animation (code sharing)

From here you can It can be seen that the position of the calf depends on the position of the thigh, so there will be no misalignment as we did above. So to put it bluntly, the characteristics of skeletal animation are:

Key elements move together with sub-elements, and sub-elements move on their own based on this.

So in js implementation, the connection is realized by first calculating the thigh position, and then calculating the calf position from the thigh position. How to implement it in css?

2.5 Pure CSS implementation

Review the most critical point: The key elements move together with the sub-elements, and the sub-elements move on their own based on this. , to realize the movement of key elements and sub-elements together, in css, as long as the key elements wrap the sub-elements! , this is the cornerstone of css to implement skeletal animation.

<div class="animate-3">
    <!--运动模块1-->
    <div class="s-1">
        <div class="item-1"></div>
        <!--运动模块2-->
        <div class="s-2">
            <div class="item-2"></div>
            <!--运动模块3-->
            <div class="s-3">
                <div class="item-3"></div>
            </div>
        </div>
    </div>
</div>
Copy after login
rrree

Beginners article: How to use CSS to implement simple skeletal animation (code sharing)

这次终于大功告成了。这里有三个元素,更多元素也是同理的,不断嵌套即可。

3、最终动效演示

细心的同学会发现上面实现的骨骼动画看着也别扭,归根结底是各个元素摆动的方向和幅度没有调节好,这里附上调整完的效果,用心感受:

.animate-4 .s-1 {
    animation: swing4-1 5s ease-in-out infinite;
    transform: rotate(-2deg);
    transform-origin: top center;
}
.animate-4 .s-2 {
    animation: swing4-2 8s ease-in-out infinite;
    transform: rotate3d(0, 1, 0, 20deg);
    transform-origin: top center;
}
.animate-4 .s-3 {
    animation: swing4-3 8s ease-in-out infinite;
    transform: rotate(3deg);
    transform-origin: top center;
}
@keyframes swing4-1 {
    0% { transform: rotate(-2deg); }
    50% { transform: rotate(2deg);}
    100% { transform: rotate(-2deg);}
}
@keyframes swing4-2 {
    0% { transform: rotate3d(0, 1, 0, 20deg); }
    50% { transform: rotate3d(0, 1, 0, -20deg);}
    100% { transform: rotate3d(0, 1, 0, 20deg);}
}
@keyframes swing4-3 {
    0% { transform: rotate(3deg); }
    50% { transform: rotate(-3deg);}
    100% { transform: rotate(3deg);}
}
Copy after login

Beginners article: How to use CSS to implement simple skeletal animation (code sharing)

4、End

纯CSS确实能实现骨骼动画,但仅限于简单的场景。在复杂场景中,例如前端游戏里面的骨骼动画,涉及到的节点比较多,用CSS虽然能实现,但效率不高,所以社区有很多从设计工具直接导出可用的骨骼动画信息,再用js来加载运行的方案,大家感兴趣可以Google一下。

本文主要通过简单的案例来加深大家对骨骼动画的原理性的认识,至于最后大家用CSS还是用JS来实现,就是“杀鸡要不要用牛刀”的问题了。

个人认为,只要屠龙刀在手,用不用已经不重要了。加油,希望大家能在各个方向找到自己的屠龙刀。

推荐学习:CSS视频教程

The above is the detailed content of Beginner's article: How to use CSS to implement simple skeletal animation (code sharing). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
css
source:juejin.im
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template