首先,定义一个最外层的容器,以及10个气泡标签,结构大致如下:
<div class="container">
<div class="bubble"></div>
<div class="bubble"></div>
<div class="bubble"></div>
<div class="bubble"></div>
<div class="bubble"></div>
<div class="bubble"></div>
<div class="bubble"></div>
<div class="bubble"></div>
<div class="bubble"></div>
<div class="bubble"></div>
</div>
然后就是给最外层的容器设置基本的样式
* {
padding: 0;
margin: 0;
}
.container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -999;
/* 设置渐变背景 */
background-image: linear-gradient(180deg,rgb(78,168,241),rgb(37,91,241));
}
大致效果如下:
接着就是设置气泡的基本样式以及气泡的动画
.bubble {
position: absolute;
border-radius: 50%;
border: 2px solid #fff;
box-shadow: inset 0 0 8px #fff;
/* 设置运动的动画 */
animation: flutter 10s infinite;
opacity: 0;
}
/* 定义动画 */
@keyframes flutter {
0% {
/* 定义动画 translateX 水平方向移动*/
transform: translateX(0);
/* 距离底部的距离 */
bottom: -100px;
/* 透明度 */
opacity: 1;
}
50% {
transform: translateX(100px);
opacity: .5;
}
100% {
transform: translateX(0);
bottom: 100%;
opacity: 0;
}
}
最后就是单独去设置每个气泡的初始位置、大小、动画总时长以及延迟时长
/* 单独设置每个气泡的初始位置、大小、以及动画的延迟时间、完成时间 */
.bubble:nth-child(1) {
left: -10%;
width: 50px;
height: 50px;
animation-duration: 9s;
animation-delay: .1s;
}
.bubble:nth-child(2) {
left: 15%;
width: 20px;
height: 20px;
animation-duration: 6s;
animation-delay: 1.5s;
}
.bubble:nth-child(3) {
left: 20%;
width: 60px;
height: 60px;
animation-duration: 9s;
}
.bubble:nth-child(4) {
left: 30%;
width: 50px;
height: 50px;
animation-duration: 5.5s;
}
.bubble:nth-child(5) {
left: 40%;
width: 50px;
height: 50px;
animation-duration: 12s;
}
.bubble:nth-child(6) {
left: 50%;
width: 20px;
height: 20px;
animation-duration: 6s;
animation-delay: 1s;
}
.bubble:nth-child(7) {
left: 60%;
width: 40px;
height: 40px;
animation-duration: 8s;
animation-delay: 1s;
}
.bubble:nth-child(8) {
left: 65%;
width: 60px;
height: 60px;
animation-duration: 15s;
animation-delay: 15s;
}
.bubble:nth-child(9) {
left: 80%;
width: 55px;
height: 55px;
animation-duration: 9s;
animation-delay: .5s;
}
.bubble:nth-child(10) {
left: 100%;
width: 40px;
height: 40px;
animation-duration: 12s;
}
源码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="css/index.css">
</head>
<body>
<div class="container">
<div class="bubble"></div>
<div class="bubble"></div>
<div class="bubble"></div>
<div class="bubble"></div>
<div class="bubble"></div>
<div class="bubble"></div>
<div class="bubble"></div>
<div class="bubble"></div>
<div class="bubble"></div>
<div class="bubble"></div>
</div>
</body>
</html>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -999;
/* 设置渐变背景 */
background-image: linear-gradient(180deg,rgb(78,168,241),rgb(37,91,241));
}
.bubble {
position: absolute;
border-radius: 50%;
border: 2px solid #fff;
box-shadow: inset 0 0 8px #fff;
/* 设置运动的动画 */
animation: flutter 10s infinite;
opacity: 0;
}
/* 定义动画 */
@keyframes flutter {
0% {
/* 定义动画 translateX 水平方向移动*/
transform: translateX(0);
/* 距离底部的距离 */
bottom: -100px;
/* 透明度 */
opacity: 1;
}
50% {
transform: translateX(100px);
opacity: .5;
}
100% {
transform: translateX(0);
bottom: 100%;
opacity: 0;
}
}
/* 单独设置每个气泡的初始位置、大小、以及动画的延迟时间、完成时间 */
.bubble:nth-child(1) {
left: -10%;
width: 50px;
height: 50px;
animation-duration: 9s;
animation-delay: .1s;
}
.bubble:nth-child(2) {
left: 15%;
width: 20px;
height: 20px;
animation-duration: 6s;
animation-delay: 1.5s;
}
.bubble:nth-child(3) {
left: 20%;
width: 60px;
height: 60px;
animation-duration: 9s;
}
.bubble:nth-child(4) {
left: 30%;
width: 50px;
height: 50px;
animation-duration: 5.5s;
}
.bubble:nth-child(5) {
left: 40%;
width: 50px;
height: 50px;
animation-duration: 12s;
}
.bubble:nth-child(6) {
left: 50%;
width: 20px;
height: 20px;
animation-duration: 6s;
animation-delay: 1s;
}
.bubble:nth-child(7) {
left: 60%;
width: 40px;
height: 40px;
animation-duration: 8s;
animation-delay: 1s;
}
.bubble:nth-child(8) {
left: 65%;
width: 60px;
height: 60px;
animation-duration: 15s;
animation-delay: 15s;
}
.bubble:nth-child(9) {
left: 80%;
width: 55px;
height: 55px;
animation-duration: 9s;
animation-delay: .5s;
}
.bubble:nth-child(10) {
left: 100%;
width: 40px;
height: 40px;
animation-duration: 12s;
}