[CSS]做一个加载动画效果的页面

HTML代码

先创建一个div,class为loading。

<!DOCTYPE html>
<html lang="en">
<head>
  <Meta charset="UTF-8">
  <Meta name="viewport" content="width=device-width, initial-scale=1.0">
  <Meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>loading动画</title>
</head>
<body>
    <div class="loading"></div>
</body>
</html>

给loading添加样式:

.loading {
  width: 200px;
  height: 200px;
  border:1px solid red; /* border方便我们观察调整代码 */
}

效果


我们使用伪元素做出一个圆形,并给它添加样式。

.loading {
  width: 200px;
  height: 200px;
  border:1px solid red;
  position: relative;
}
​
.loading::before { /* 伪元素 */
  content:'';
  width: 10px;
  height: 10px;
  border-radius:50%;
  background-color: black;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto; /* 绝对定位,上下左右都设置为0,margin为auto就可以居中 */
}

效果


现在我们先给这个圆形做一个简单的动画:

.loading {
  width: 200px;
  height: 200px;
  border: 1px solid red;
  position: relative;
}
​
.loading::before {
  content: '';
  width: 0px; /* 可以改为0 */
  height: 0px; /* 可以改为0 */
  border-radius: 50%;
  background-color: black;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
  animation: linearSize 1.5s linear infinite; /* 动画名字,时间和效果 */
}
​
@keyframes linearSize { /* 动画的样式过渡,0%到100%,宽高和透明度的变化 */
  0% {
    width: 0;
    height: 0;
    opacity: 1;
  }
  100% {
    width: 100px;
    height: 100px;
    opacity: 0;
  }
}

效果


这里的重要点就是animation动画的运用。同样我们可以再使用伪元素after做出相同的圆形。

设置另一个圆形的延迟动画效果,让两个圆形的动画时间有时差,这样就能表现出页面载入的效果

.loading {
  width: 200px;
  height: 200px;
  border: 1px solid red;
  position: relative;
}
​
.loading::before, .loading::after {
  content: '';
  width: 0px;
  height: 0px;
  border-radius: 50%;
  background-color: black;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
  animation: linearSize 1.5s linear infinite;
}
​
.loading::after {
  animation-delay: 0.75s; /* 表示动画效果在0.75s后开始 */
}
​
@keyframes linearSize {
  0% {
    width: 0;
    height: 0;
    opacity: 1;
  }
  100% {
    width: 100px;
    height: 100px;
    opacity: 0;
  }
}

然后我们在class为loading的div外再套一层div,让class为loading的div居中。


<!DOCTYPE html>
<html lang="en">
<head>
  <Meta charset="UTF-8">
  <Meta name="viewport" content="width=device-width, initial-scale=1.0">
  <Meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>loading动画</title>
</head>
<body>
    <div class="container">
        <div class="loading"></div>
    </div>
</body>
</html>
.loading {
  width: 200px;
  height: 200px;
  border: 1px solid red;
  position: relative;
}
​
.loading::before, .loading::after {
  content: '';
  width: 0px;
  height: 0px;
  border-radius: 50%;
  background-color: black;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
  animation: linearSize 1.5s linear infinite;
}
​
.loading::after {
  animation-delay: 0.75s; /* 表示动画效果在0.75s后开始 */
}
​
@keyframes linearSize {
  0% {
    width: 0;
    height: 0;
    opacity: 1;
  }
  100% {
    width: 100px;
    height: 100px;
    opacity: 0;
  }
}
​
.container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #888;
  display: -webkit-flex; /* 添加后缀名,适应不同浏览器显示 */
  display: -moz-flex;
  display: -ms-flex;
  display: -o-flex;
  display: flex; /* flex布局 */
  justify-content: center;
  align-items: center;
}

最终效果

相关文章

Css3如何实现鼠标移上变长特效?(图文+视频)
css3怎么实现鼠标悬停图片时缓慢变大效果?(图文+视频)
jquery如何实现点击网页回到顶部效果?(图文+视频)
css3边框阴影效果怎么做?(图文+视频)
css怎么实现圆角边框和圆形效果?(图文+视频教程)
Css3如何实现旋转移动动画特效