CSS动画实例:10个有关五角星的动画特效
CSS动画实例:10个有关五角星的动画特效
设页面中有
,若定义. star的样式规则如下:.star
{
position: absolute;
display: block;
width:0px;
height:0px;
left:calc(50% - 50px);
top:calc(50% - 23px);
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom:35px solid red;
transform:rotate(35deg);
}
.star:before
{
content: '';
position: absolute;
width: 0px;
height: 0px;
top: -22.5px;
left: -31.25px;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
border-bottom: 40px solid red;
transform: rotate(-35deg);
}
.star:after
{
content: '';
position: absolute;
width: 0px;
height: 0px;
top: 1.5px;
left: -52.5px;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 35px solid red;
transform:rotate(-70deg);
}
可在页面中绘制出如图1所示的五角星。
图1 五角星
下面将1个或多个如图1所示的五角星作为基本动画元素,为它们定义关键帧,使这些五角星产生动画效果。
1.大小五角星切换
在页面中放置两个
,定义关键帧,使得一个五角星放大倍数更大些,且在放大过程中,透明度也发生改变;另一个五角星放大倍数小些,在放大过程中透明度不变,从而呈现出大小两个五角星切换的效果。编写的HTML文件如下。
.container
{
position: relative;
margin: 50px auto;
width: 300px;
height:300px;
background:#d8d8d8;
border: 4px solid rgba(255, 0, 0, 0.9);
border-radius: 10%;
}
.star
{
position: absolute;
display: block;
width:0px;
height:0px;
left:calc(50% - 50px);
top:calc(50% - 23px);
border-left: 25px solid transparent;
border-right: 25px solid transparent;
border-bottom:17.5px solid red;
transform:rotate(35deg);
}
.star:before
{
content: '';
position: absolute;
width: 0px;
height: 0px;
top: -11.25px;
left: -15.625px;
border-left: 7.5px solid transparent;
border-right: 7.5px solid transparent;
border-bottom: 20px solid red;
transform: rotate(-35deg);
}
.star:after
{
content: '';
position: absolute;
width: 0px;
height: 0px;
top: 0.75px;
left: -26.25px;
border-left: 25px solid transparent;
border-right: 25px solid transparent;
border-bottom: 17.5px solid red;
transform:rotate(-70deg);
}
.star:nth-child(1)
{
animation: star-small 1s ease-in-out alternate infinite; }
.star:nth-child(2)
{
animation: star-big 2s -0.2s ease-in-out infinite;
}
@keyframes star-big
{
0%
{
transform: rotate(180deg) scale(0);
opacity: 1;
}
100%
{
transform: rotate(180deg) scale(5);
opacity: 0;
}
}
@keyframes star-small
{
0% { transform: rotate(180deg) scale(1); }
100% { transform: rotate(180deg) scale(2); }
}
在浏览器中打开包含这段HTML代码的html文件,可以呈现出如图2所示的动画效果。
图2 大小五角星切换
2.五角星分开与合拢
在页面中放置四个五角星,初始时4个五角星在同一个位置重叠起来,之后各五角星分别向上下左右四个方向移动,4个五角星分开,分开后又移动回到初始的中间位置而合拢。
编写的HTML文件如下。
.container
{
position: relative;
margin: 50px auto;
width: 300px;
height:300px;
background:#d8d8d8;
border: 4px solid rgba(255, 0, 0, 0.9);
border-radius: 10%;
}
.star
{
position: absolute;
display: block;
width:0px;
height:0px;
left:calc(50% - 50px);
top:calc(50% - 23px);
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom:35px solid red;
transform:rotate(35deg);
}
.star:before
{
content: '';
position: absolute;
width: 0px;
height: 0px;
top: -22.5px;
left: -31.25px;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
border-bottom: 40px solid red;
transform: rotate(-35deg);
}
.star:after
{
content: '';
position: absolute;
width: 0px;
height: 0px;
top: 1.5px;
left: -52.5px;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 35px solid red;
transform:rotate(-70deg);
}
.star
{
animation: star 2s linear alternate infinite; }
.star:nth-child(1)
{
top: 125px;
left: 20px;
}
.star:nth-child(2)
{
top: 125px;
left: 180px;
}
.star:nth-child(3)
{
top: 45px;
left: 100px;
}
.star:nth-child(4)
{
top: 205px;
left: 100px;
}
@-webkit-keyframes star
{
0%
{
top: 125px;
left: 100px;
}
100% { }
}
在浏览器中打开包含这段HTML代码的html文件,可以呈现出如图3所示的动画效果。
图3 五角星分开与合拢
3.同行五角星缩放
在页面一行中放置4个五角星,每个五角星按给定延迟进行缩放,在缩放的同时透明度发生变化。
编写的HTML文件如下。
.container
{
position: relative;
margin: 50px auto;
width: 300px;
height:300px;
background:#d8d8d8;
border: 4px solid rgba(255, 0, 0, 0.9);
border-radius: 10%;
}
.star
{
position: absolute;
display: block;
width:0px;
height:0px;
left:calc(50% - 50px);
top:calc(50% - 23px);
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom:35px solid red;
transform:rotate(35deg);
}
.star:before
{
content: '';
position: absolute;
width: 0px;
height: 0px;
top: -22.5px;
left: -31.25px;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
border-bottom: 40px solid red;
transform: rotate(-35deg);
}
.star:after
{
content: '';
position: absolute;
width: 0px;
height: 0px;
top: 1.5px;
left: -52.5px;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 35px solid red;
transform:rotate(-70deg);
}
.star
{
animation: star 2s linear alternate infinite; }
.star:nth-child(1)
{
animation: star 1s ease-in-out infinite;
left: 0;
}
.star:nth-child(2)
{
animation: star 1s 0.2s ease-in-out infinite;
left: 70px;
}
.star:nth-child(3)
{
animation: star 1s 0.4s ease-in-out infinite;
left: 140px;
}
.star:nth-child(4)
{
animation: star 1s 0.6s ease-in-out infinite;
left: 210px;
}
@keyframes star
{
0%,100%
{
transform: scale(0.8);
opacity: 1;
}
80%
{
transform: scale(0);
opacity: 0;
}
}
在浏览器中打开包含这段HTML代码的html文件,可以呈现出如图4所示的动画效果。
图4 同行五角星的缩放
4.成圆周分布五角星的缩放
在页面一行中放置8个五角星,使这8个五角星成圆周分布,每个五角星按给定延迟进行缩放,在缩放的同时透明度发生变化。
编写的HTML文件如下。
.container
{
position: relative;
margin: 50px auto;
width: 300px;
height:300px;
background:#d8d8d8;
border: 4px solid rgba(255, 0, 0, 0.9);
border-radius: 10%;
}
.star
{
position: absolute;
display: block;
width:0px;
height:0px;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom:35px solid red;
transform:rotate(35deg);
}
.star:before
{
content: '';
position: absolute;
width: 0px;
height: 0px;
top: -22.5px;
left: -31.25px;
border-left: 15px solid transparent; border-right: 15px solid transparent; border-bottom: 40px solid red; transform: rotate(-35deg);
}
.star:after
{
content: '';
position: absolute;
width: 0px;
height: 0px;
top: 1.5px;
left: -52.5px;
border-left: 50px solid transparent; border-right: 50px solid transparent; border-bottom: 35px solid red; transform:rotate(-70deg);
}
.star
{
transform: rotate(180deg) scale(0.5);
opacity: 0.4;
}
.star:nth-child(1)
{
top: 60px;
left: 110px;
animation: anim 1s 0.125s ease infinite; }
.star:nth-child(2)
{
left: 150px;
top: 80px;
animation: anim 1s 0.3s ease infinite; }
.star:nth-child(3)
{
left:170px;
top:120px;
animation: anim 1s 0.425s ease infinite;
}
.star:nth-child(4)
{
top: 160px;
left: 150px;
animation: anim 1s 0.540s ease infinite;
}
.star:nth-child(5)
{
top: 180px;
left: 110px;
animation: anim 1s 0.665s ease infinite;
}
.star:nth-child(6)
{
top: 160px;
left: 70px;
animation: anim 1s 0.79s ease infinite;
}
.star:nth-child(7)
{
top: 120px;
left: 50px;
animation: anim 1s 0.9s ease infinite;
}
.star:nth-child(8)
{
top: 80px;
left: 70px;
animation: anim 1s 1s ease infinite;
}
@keyframes anim
{
0%,100%
{
opacity: 0.2;
transform: rotate(180deg) translate(0px, 0) scale(0.6);
}
25%
{
opacity: 0.2;
transform: rotate(180deg) translate(0, 0) scale(0.2);
}
50%
{
opacity: 0.8;
transform: rotate(180deg) translate(5px, 5px) scale(0.4);
}
75%
{
opacity: 0.2;
transform: rotate(180deg) translate(0, 0) scale(0.6);
}
80%
{
opacity: 1;
transform: rotate(180deg) translate(5px, 0) scale(0.1);
}
}
在浏览器中打开包含这段HTML代码的html文件,可以呈现出如图5所示的动画效果。
图5 圆周分布的五角星缩放
5.五角星旋转展开与收缩
在页面中放置一个五角星(
)和一个圆周(),为五角星定义关键帧,使得五角星在旋转的同时缩放,为圆周定义关键帧,使得圆周在放大的同时,通过阴影的方式在圆周上绘制16个大小相间隔的实心圆。两者配合起来,呈现出五角星旋转展开与收缩的动画效果。编写的HTML文件如下。
.container
{
position: relative;
margin: 50px auto;
width: 300px;
height:300px;
background:#d8d8d8;
border: 4px solid rgba(255, 0, 0, 0.9);
border-radius: 10%;
}
.star
{
position: absolute;
display: block;
width:0px;
height:0px;
left:calc(50% - 50px);
top:calc(50% - 23px);
border-left: 50px solid transparent; border-right: 50px solid transparent; border-bottom:35px solid red; transform:rotate(35deg);
}
.star:before
{
content: '';
position: absolute;
width: 0px;
height: 0px;
top: -22.5px;
left: -31.25px;
border-left: 15px solid transparent; border-right: 15px solid transparent; border-bottom: 40px solid red; transform: rotate(-35deg);
}
.star:after
{
content: '';
position: absolute;
width: 0px;
height: 0px;
top: 1.5px;
left: -52.5px;
border-left: 50px solid transparent; border-right: 50px solid transparent; border-bottom: 35px solid red; transform:rotate(-70deg);
}
.star
{
animation: star 1s ease alternate infinite; }
.circles
{
width: 8px;
height: 8px;
background: #f00;
border-radius: 50%;
position: absolute;
left: calc(50% - 4px);
top: calc(50% - 4px);
transition: all 1s ease;
animation: circles 1s ease-in-out alternate infinite;
}
@keyframes circles
{
0%
{
box-shadow: 0 0 0 #f0f;
opacity: 1;
transform: rotate(0deg);
}
50%
{
box-shadow: 24px -22px #f0f, 30px -15px 0 -3px #f0f, 31px 0px #f0f,
13px -25px 0 -3px #f0f,29px 9px 0 -3px #f0f, 24px 23px #f0f,
17px 30px 0 -3px #f0f, 0px 33px #f0f,-10px 28px 0 -3px #f0f,
-24px 22px #f0f, -29px 14px 0 -3px #f0f, -31px -3px #f0f,
-30px -11px 0 -3px #f0f, -20px -25px #f0f, -12px -30px 0 -3px #f0f,
5px -29px #f0f;
transform: scale(2.5) rotate(180deg);
}
100%
{
opacity: 0;
transform: scale(2.5) rotate(360deg);
box-shadow: 25px -22px #f0f, 15px -22px 0 -3px black, 31px 2px #f0f,
21px 2px 0 -3px black, 23px 25px #f0f, 13px 25px 0 -3px black,
0px 33px #f0f, -10px 33px 0 -3px black,-26px 24px #f0f,
-19px 17px 0 -3px black, -32px 0px #f0f, -23px 0px 0 -3px black,
-25px -23px #f0f, -16px -23px 0 -3px black, 0px -31px #f0f,
-2px -23px 0 -3px black;
}
}
@keyframes star
{
0%
{
transform: scale(0) rotate(0deg);
}
100%
{
transform: scale(1.5) rotate(360deg);
}
}
在浏览器中打开包含这段HTML代码的html文件,可以呈现出如图6所示的动画效果。
图6 五角星旋转展开与收缩
6.五角星旋转放光芒
还是在页面中放置一个五角星(
)和一个圆周(),为五角星定义关键帧,使得五角星进行旋转;圆周通过圆点边框的方式绘制,为圆周定义关键帧,使得圆周放大。两者配合起来,呈现出五角星旋转放光芒的动画效果。编写的HTML文件如下。
.container
{
position: relative;
margin: 50px auto;
width: 300px;
height:300px;
background:#d8d8d8;
border: 4px solid rgba(255, 0, 0, 0.9);
border-radius: 10%;
}
.star
{
position: absolute;
display: block;
width:0px;
height:0px;
left:calc(50% - 50px);
top:calc(50% - 23px);
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom:35px solid red;
transform:rotate(35deg);
}
.star:before
{
content: '';
position: absolute;
width: 0px;
height: 0px;
top: -22.5px;
left: -31.25px;
border-left: 15px solid transparent;
border-right: 15px solid transparent;
border-bottom: 40px solid red;
transform: rotate(-35deg);
}
.star:after
{
content: '';
position: absolute;
width: 0px;
height: 0px;
top: 1.5px;
left: -52.5px;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 35px solid red;
transform:rotate(-70deg);
}
.star
{
animation: star 1s linear infinite;
}
.circles
{
left: calc(50% - 40px);
top: calc(50% - 45px);
position:absolute;
height:70px;
width:70px;
border-radius: 50%;
border: 5px dashed rgba(255, 0, 0, 0.5);
animation:anim .25s linear infinite;
}
@keyframes anim
{
100%
{
transform:scale(2.5);
border-width: 0;
}
}
@keyframes star
{
0% { transform:rotate(0deg); }
50% { transform:rotate(180deg); }
100% { transform:rotate(360deg); }
}
在浏览器中打开包含这段HTML代码的html文件,可以呈现出如图7所示的动画效果。
图7 五角星旋转放光芒
7.旋转的五角星链
用6个五角星组成一个五角星链,让这个五角星链旋转起来,旋转的过程中,6个五角星还会收缩成一个五角星。
编写的HTML文件如下。
.container
{
position: relative;
margin: 50px auto;
width: 300px;
height:300px;
background:#d8d8d8;
border: 4px solid rgba(255, 0, 0, 0.9);
border-radius: 10%;
}
.star
{
position: absolute;
display: block;
width:0px;
height:0px;
left:calc(50% - 50px);
top:calc(50% - 23px);
border-left: 25px solid transparent;
border-right: 25px solid transparent;