一闪一闪小幽灵
突发奇想做一个小幽灵的动画,用纯css就可以实现,主要是利用的css的关键帧动画@keyframes可以循环播放动画来实现的。
而html方面则是用div的多层嵌套实现的。
话不多说,直接看代码把,代码还是非常简单易懂的,相信又要有前端基础的人都能看得懂。
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>小幽灵</title>
<link rel="stylesheet" href="/static/my/css/ghost.css">
</head>
<body>
<div class='ghost-box'>
<div class='symbol'></div>
<div class='symbol'></div>
<div class='symbol'></div>
<div class='symbol'></div>
<div class='symbol'></div>
<div class='symbol'></div>
<div class='ghost'>
<div class='ghost-eyes'>
<div class='eye-left'></div>
<div class='eye-right'></div>
</div>
<div class='ghost-footer'>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</div>
<div class='ghost-shadow'></div>
</div>
</body>
</html>
CSS
@keyframes ghost-float {
0%,
to {
opacity: 0;
transform: translateY(5px) scale(0.9);
}
50% {
opacity: 1;
transform: translateY(25px) scale(1);
}
}
@keyframes ghost-shadow {
0%,
to {
opacity: 0;
transform: scale(0.8);
}
50% {
opacity: 1;
transform: scale(1);
}
}
@keyframes ghost-eyes {
0%,
to {
left: 0;
}
40%,
60% {
left: 30%;
}
}
@keyframes shine {
0%,
to {
box-shadow: #fff 0 0 5px;
opacity: 1;
}
50% {
box-shadow: #fff 0 0;
opacity: 0;
}
}
:root {
--color-eyes: white;
--color-body: #d4e8ee;
--color-background: #131a24;
--color-shadow: rgb(230, 211, 211);
background-color: var(--color-background);
}
html {
height: 100%;
}
body {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.ghost-box {
padding: 15px 25px 25px;
width: 150px;
position: relative;
box-sizing: border-box;
* {
box-sizing: border-box;
}
.ghost {
background: var(--color-body);
width: 100px;
border-radius: 100px 100px 0 0;
position: relative;
margin: 0 auto;
overflow: hidden;
padding-bottom: 20px;
height: 130px;
background-clip: content-box;
animation: ghost-float 8s ease-in-out infinite;
.ghost-eyes {
position: absolute;
left: 50%;
top: 30%;
height: 12px;
width: 70px;
transition: all 0.05s ease-out;
animation: ghost-eyes 5s ease-in-out infinite;
.eye-left, .eye-right {
width: 12px;
height: 12px;
background: var(--color-eyes);
border-radius: 50%;
margin: 0 10px;
position: absolute;
}
.eye-left {
left: 0;
}
.eye-right {
right: 0;
}
}
.ghost-footer {
display: flex;
position: absolute;
left: 0;
right: 0;
bottom: 0;
div {
flex-grow: 1;
position: relative;
top: -10px;
height: 20px;
border-radius: 100%;
background-color: var(--color-body);
&:nth-child(2n) {
top: -12px;
margin: 0 0;
border-top: 15px var(--color-background);
background: transparent;
}
}
}
}
.ghost-shadow {
height: 20px;
box-shadow: 0 50px 15px 5px var(--color-shadow);
border-radius: 50%;
margin: 0 auto;
animation: ghost-shadow 8s ease-in-out infinite;
}
.symbol:nth-child(1) {
opacity: 0.2;
animation: shine 4s ease-in-out 3s infinite;
}
.symbol:nth-child(1):before, .symbol:nth-child(1):after {
content: '';
width: 12px;
height: 4px;
background: var(--color-body);
position: absolute;
border-radius: 5px;
bottom: 65px;
left: 0;
}
.symbol:nth-child(1):before {
transform: rotate(45deg);
}
.symbol:nth-child(1):after {
transform: rotate(-45deg);
}
.symbol:nth-child(2) {
position: absolute;
left: -5px;
top: 30px;
height: 18px;
width: 18px;
border: 4px solid;
border-radius: 50%;
border-color: var(--color-body);
opacity: 0.2;
animation: shine 4s ease-in-out 1.3s infinite;
}
.symbol:nth-child(3) {
opacity: 0.2;
animation: shine 3s ease-in-out 0.5s infinite;
}
.symbol:nth-child(3):before, .symbol:nth-child(3):after {
content: '';
width: 12px;
height: 4px;
background: var(--color-body);
position: absolute;
border-radius: 5px;
top: 5px;
left: 40px;
}
.symbol:nth-child(3):before {
transform: rotate(90deg);
}
.symbol:nth-child(3):after {
transform: rotate(180deg);
}
.symbol:nth-child(4) {
opacity: 0.2;
animation: shine 6s ease-in-out 1.6s infinite;
}
.symbol:nth-child(4):before, .symbol:nth-child(4):after {
content: '';
width: 15px;
height: 4px;
background: var(--color-body);
position: absolute;
border-radius: 5px;
top: 10px;
right: 30px;
}
.symbol:nth-child(4):before {
transform: rotate(45deg);
}
.symbol:nth-child(4):after {
transform: rotate(-45deg);
}
.symbol:nth-child(5) {
position: absolute;
right: 5px;
top: 40px;
height: 12px;
width: 12px;
border: 3px solid;
border-radius: 50%;
border-color: var(--color-body);
opacity: 0.2;
animation: shine 1.7s ease-in-out 7s infinite;
}
.symbol:nth-child(6) {
opacity: 0.2;
animation: shine 2s ease-in-out 6s infinite;
}
.symbol:nth-child(6):before, .symbol:nth-child(6):after {
content: '';
width: 15px;
height: 4px;
background: var(--color-body);
position: absolute;
border-radius: 5px;
bottom: 65px;
right: -5px;
}
.symbol:nth-child(6):before {
transform: rotate(90deg);
}
.symbol:nth-child(6):after {
transform: rotate(180deg);
}
}
这里的CSS我是用SCSS预编译的,所以需要大家先去安装SCSS才能使用