「HTML+CSS」自定义加载动画【030】

海轰Pro

共 3239字,需浏览 7分钟

 · 2021-05-05

效果展示

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{
  margin0;
  height100%;
}
body{
  display: flex;
  justify-content: center;
  align-items: center;
  background#263238;
}
section {
    width650px;
    height300px;
    padding10px;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    border2px solid red;
}
span {
  width 96px;
  height96px;
  position: relative;
}
span::before,
span::after{
  position: absolute;
  content'';
  width96px;
  height96px;
  top0;
  left0;
  background: white;
  border-radius50%;
  animation: animloader 2s linear infinite;
}
span::after{
  animation-delay1s;
}
@keyframes animloader {
  0% { transformscale(0); opacity1;}
  100% { transformscale(1); opacity0;}
}

原理详解

步骤1

使用span标签,设置

  • 宽度、高度均为96px
  width: 96px;
  height: 96px;

步骤2

使用span::before、span::after伪类元素充当白色波纹

设置

  • 绝对定位(top:0 left:0)
  • 宽度、高度均为96px(这样就是与原span重叠了)
  • 颜色:白色
 position: absolute;
  content: '';
  width: 96px;
  height: 96px;
  top: 0;
  left: 0;
  background: white;

效果图如下

步骤3

span::before、span::after圆角化

border-radius: 50%;

效果图如下

注:这里span、span::before、span::after三者都重叠在同一个位置了,其中span无颜色,其余为白色

步骤4

为span::before、span::after添加动画

  • 初始状态:大小为0(相对于原大小),颜色为(白,透明级别1)
  • 终止状态:大小为1(相对于原大小),颜色为(白,透明级别0)
animation: animloader 2s linear infinite;
/*动画实现*/
@keyframes animloader {
  0% {
    transform: scale(0);
    opacity: 1;
  }
  100% {
    transform: scale(1);
    opacity: 0;
  }
}

效果图如下

步骤5

为了使span::before、span::after二者之间的动画产生视觉差效果

对span::after进行延时

这样同一时刻就可以看到两个圆的变换了

animation-delay: 1s;

效果图如下

结语

学习来源:

https://codepen.io/bhadupranjal/pen/vYLZYqQ

文章仅作为学习笔记,记录从0到1的一个过程。

希望对您有所帮助,如有错误欢迎小伙伴指正~

我是 海轰ଘ(੭ˊᵕˋ)੭

如果您觉得写得可以的话请点个赞吧

谢谢支持❤️


浏览 27
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报