search
HomeWeb Front-endCSS TutorialBeginner's article: How to use CSS to implement simple skeletal animation (code sharing)

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);}
}

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>

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>
.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);}
}

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>
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);}
}

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!

Statement
This article is reproduced at:掘金. If there is any infringement, please contact admin@php.cn delete
利用CSS怎么创建渐变色边框?5种方法分享利用CSS怎么创建渐变色边框?5种方法分享Oct 13, 2021 am 10:19 AM

利用CSS怎么创建渐变色边框?下面本篇文章给大家分享CSS实现渐变色边框的5种方法,希望对大家有所帮助!

css ul标签怎么去掉圆点css ul标签怎么去掉圆点Apr 25, 2022 pm 05:55 PM

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

css与xml的区别是什么css与xml的区别是什么Apr 24, 2022 am 11:21 AM

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

css3怎么实现鼠标隐藏效果css3怎么实现鼠标隐藏效果Apr 27, 2022 pm 05:20 PM

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

css怎么实现英文小写转为大写css怎么实现英文小写转为大写Apr 25, 2022 pm 06:35 PM

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

rtl在css是什么意思rtl在css是什么意思Apr 24, 2022 am 11:07 AM

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

css怎么设置i不是斜体css怎么设置i不是斜体Apr 20, 2022 am 10:36 AM

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

怎么设置rotate在css3的旋转中心点怎么设置rotate在css3的旋转中心点Apr 24, 2022 am 10:50 AM

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!