使用css+div 制作类似gif动画效果-兔子拜年动画,这里有给anim设置的动画小技巧就是如何让动画层次分明,以及动画的流畅性,效果如下gif图
看官觉得有趣,觉得有帮助或者对各位以后坐css动画有启发可以点个赞收藏下,大佬请勿喷,手下留情!
废话不多说先上代码
1 结构如图
2个结构 ,第一个分层好的图片(这块需要ps帮助,有动画制作经验的更好)第二个式main.css文件
首先先看第一个img包结构
里面分层了每个动画部件,然后按照下面的div结构排列如图
那么基础准备好了,下面就是重点css的设置
首先要把兔子的基本形状摆好
代码如下:
.rabbit-box {
width: 353px;
height: 314px;
position: relative;
top: 30px;
left: 40px
}
/* 最上面的耳朵,深度层级为1-因为这只耳朵在最后面被其他兔子层级挡住*/
.rabbit-upEar {
position: absolute;
z-index: 1;
width: 95px;
height: 41px;
background-image: url('../img/rabbit/upEar.png');
background-position: center;
background-repeat: no-repeat;
background-size: contain;
animation: points-move-coordinate-top-upEar 2s infinite;
}
/* 短飘带层,深度层级为2*/
.rabbit-shortStreamer {
position: absolute;
z-index: 2;
width: 84px;
height: 25px;
background-image: url('../img/rabbit/shortStreamer.png');
background-position: center;
background-repeat: no-repeat;
background-size: contain;
animation: points-move-longStreamer 2s infinite;
}
/*兔子身体,深度层级为3*/
.rabbit-body {
position: absolute;
z-index: 3;
width: 353px;
height: 314px;
background-image: url('../img/rabbit/rabbit_body.png');
background-position: center;
background-repeat: no-repeat;
background-size: contain;
animation: points-move-coordinate-top 2s infinite;
}
/*下面的耳朵,深度层级为4*/
.rabbit-downEar {
position: absolute;
z-index: 4;
width: 98px;
height: 46px;
background-image: url('../img/rabbit/upEar.png');
background-position: center;
background-repeat: no-repeat;
background-size: contain;
animation: points-move-coordinate-top-downEar 2s infinite;
}
/* 长飘带层,深度层级为5 */
.rabbit-longStreamer {
position: absolute;
z-index: 5;
width: 214px;
height: 72.5px;
background-image: url('../img/rabbit/longstreamer.png');
background-position: center;
background-repeat: no-repeat;
background-size: contain;
animation: points-move-longStreamer 2s infinite;
}
/* 灯笼手层,深度层级为6 */
.rabbit-lantern {
position: absolute;
z-index: 6;
width: 168px;
height: 117px;
background-image: url('../img/rabbit/lantern.png');
background-position: center;
background-repeat: no-repeat;
background-size: contain;
animation: points-move-coordinate-top-lantern 2s infinite;
}
开始设置动画属性,想让兔子的身体有个飘动的效果,就必须有个概念,就是动画部件必须根据一个中心点来上下或者左右摆动,那么在css里面这里有个重要的设置transform-origin,这个可以设置中心点,div以这个中心点为基础上下或者左右运动,下面飘带的png图,设置transform-origin: 0 100%; 表示中心点 如图:
关于如何准确设置需要调试具体可以看一个博客,里面写的非常清楚,有空可以多了解下,
第二个细节就是在3个铜钱动画里面有个环绕的效果,是钱币在动画时间的进程中,百分比进度通过设置z-index 变化层级来实现,简单来说就是钱币在通过兔子身体后面的时候,马上设置z-index比最低的层级还小就可以让兔子身体覆盖住钱币,然后整体动画再设置一下钱币大小变化形成一个由近至远的环绕兔子的视觉效果 。具体代码如下
@keyframes points-move-longStreamer {
0% {
top: 181px;
left: 135px;
transform: rotate(5deg);
transform-origin: 0 100%;
}
50% {
top: 181px;
left: 135px;
transform: rotate(0deg);
transform-origin: 0 100%;
}
100% {
top: 181px;
left: 135px;
transform: rotate(5deg);
transform-origin: 0 100%;
}
}
@keyframes points-move-coordinate-top {
0% {
transform: translateY(0px);
}
50% {
transform: translateY(10px);
}
100% {
transform: translateY(0px);
}
}
@keyframes points-move-coordinate-top-lantern {
0% {
top: 109px;
transform: rotate(5deg) scale(1);
transform-origin: 100% 50%;
}
50% {
top: 110px;
transform: rotate(0deg) scale(1.1);
transform-origin: 100% 50%;
}
100% {
top: 109px;
transform: rotate(5deg) scale(1);
transform-origin: 100% 50%;
}
}
@keyframes points-move-coordinate-top-upEar {
0% {
top: 14px;
left: 138px;
transform: rotate(5deg);
transform-origin: 10% 100%;
}
50% {
top: 24px;
left: 138px;
transform: rotate(20deg);
transform-origin: 10% 100%;
}
100% {
top: 14px;
left: 138px;
transform: rotate(5deg);
transform-origin: 10% 100%;
}
}
@keyframes points-move-coordinate-top-downEar {
0% {
top: 53px;
left: 180px;
transform: rotate(15deg);
transform-origin: 0 100%;
}
50% {
top: 59px;
left: 180px;
transform: rotate(0deg);
transform-origin: 0 100%;
}
100% {
top: 53px;
left: 180px;
transform: rotate(15deg);
transform-origin: 0 100%;
}
}
@keyframes points-move-coordinate-top-coin1 {
0% {
top: 1px;
left: 80px;
z-index: 6;
transform: rotate(0deg) scale(0.8);
}
50% {
top: 85px;
left: 280px;
z-index: 6;
transform: rotate(360deg) scale(1.4);
}
60% {
z-index: 0;
transform: scale(1.1);
}
80% {
z-index: 0;
}
100% {
top: 5px;
left: 80px;
z-index: 6;
transform: rotate(0deg) scale(0.8);
}
}
@keyframes points-move-coordinate-top-coin2 {
0% {
z-index: 6;
top: 150px;
left: 270px;
transform: rotate(0deg) scale(1.3);
}
20% {
z-index: 0;
transform: scale(0.9);
}
50% {
z-index: 0;
top: 50px;
left: 10px;
transform: rotate(180deg) scale(0.7);
}
60% {
z-index: 6;
transform: scale(1.1);
}
100% {
z-index: 6;
top: 150px;
left: 270px;
transform: rotate(0deg) scale(1.3);
}
}
@keyframes points-move-coordinate-top-coin3 {
0% {
z-index: 0;
top: 135px;
left: 150px;
transform: rotate(0deg);
}
50% {
z-index: 4;
top: 125px;
left: 250px;
transform: rotate(180deg);
}
100% {
z-index: 0;
top: 135px;
left: 150px;
transform: rotate(0deg);
}
}
好,整个兔子动画就完成了,希望能给到大家启发和帮助,谢谢观看!
需要源码可以留言!(新手暂时不知道怎么放,研究研究)