前言
Hello!小伙伴!
 非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~
  
 自我介绍 ଘ(੭ˊᵕˋ)੭
 昵称:海轰
 标签:程序猿|C++选手|学生
 简介:因C语言结识编程,随后转入计算机专业,有幸拿过国奖、省奖等,已保研。目前正在学习C++/Linux(真的真的太难了~)
 学习经验:扎实基础 + 多做笔记 + 多敲代码 + 多思考 + 学好英语!
  
【动画消消乐】
效果展示

Demo代码
HTML
<!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">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<section><span></span></section>
</body>
</html>
CSS
html, body {
  margin: 0;
  height: 100%;
}
body {
  display: flex;
  justify-content: center;
  align-items: center;
  background: #222f3e;
}
section {
  width: 650px;
  height: 300px;
  padding: 10px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid white;
}
span {
  width: 100%;
  height: 10px;
  display: inline-block;
  position: relative;
  background: rgba(255, 255, 255, 0.15);
  overflow: hidden;
}
span::after {
  content: '';
  width: 0;
  height: 10px;
  background: white;
  position: absolute;
  top: 0;
  left: 0;
  animation: loading 4s linear infinite;
}
@keyframes loading {
  0% {
    width: 0
  }
  100% {
    width: 100%
  }
}原理详解
步骤1
使用一个span标签
<span></span>
设置为:
- 宽度100% 高度10px
 - 相对定位
 - 背景颜色:白色 透明级别为0.15
 
span {
  width: 100%;
  height: 10px;
  position: relative;
  background: rgba(255, 255, 255, 0.15);
  }效果图如下:

步骤2
借助span::after充当白色条状部分
设置为:
- 宽度:0px 高度:10px
 - 背景颜色:白色
 - 绝对定位( top: 0; left: 0;)(放在span最左边)
 
span::after {
  content: '';
  width: 0px;
  height: 10px;
  background: white;
  position: absolute;
  top: 0;
  left: 0;
  }效果图如下:

 注:span::after宽度为0 其实是看不到这个白色部分的。这里海轰只是为了显示出after的位置将其宽度设置为10px 便于确定其位置
步骤3
为span::after添加动画
效果很简单
就是span::after的白色逐步填充完span
本质就是span的宽度从0到100%
span::after {
  animation: loading 4s linear infinite;
  }
  
  @keyframes loading {
  0% {
    width: 0
  }
  100% {
    width: 100%
  }
  }得到目标效果:

结语
文章仅作为学习笔记,记录从0到1的一个过程
希望对您有所帮助,如有错误欢迎小伙伴指正~
我是 海轰ଘ(੭ˊᵕˋ)੭

                










