微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[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;
}

最终效果

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。