0
点赞
收藏
分享

微信扫一扫

【动画消消乐】HTML+CSS 自定义加载动画 070


前言

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

效果展示

【动画消消乐】HTML+CSS 自定义加载动画 070_学习笔记

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: #ed556a;
/* background-color: #82466e; */
animation: backColor 4s infinite;
}

section {
width: 650px;
height: 300px;
padding: 10px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
border: 2px solid white;
}

span {
width: 5px;
height: 5px;
display: inline-block;
position: relative;
border-radius: 4px;
color: white;
background: currentColor;
animation: animloader66 0.6s 0.6s linear infinite alternate;
}

span::before, span::after {
content: '';
width: 5px;
height: 5px;
border-radius: 4px;
background: currentColor;
position: absolute;
left: 0;
top: 15px;
animation: animloader66 0.6s 0.9s linear infinite alternate;
}

[data-index="66"] {
justify-content: normal;
padding-left: 15%;
}

span::after {
top: -15px;
animation-delay: 0s;
}

@keyframes animloader66 {
0% {
width: 5px;
}
100% {
width: 48px;
}
}

原理详解

步骤1

使用span标签,设置为

  • 相对定位
  • 宽度、高度均为5px
  • 背景色:白色
  • color:白色
  • border-radius: 4px

span {
width: 5px;
height: 5px;
position: relative;
border-radius: 4px;
color: white;
background: white;
}

效果图如下

【动画消消乐】HTML+CSS 自定义加载动画 070_自定义加载动画_02

步骤2

为span添加动画

动画描述效果为:span宽度从5px变化到48px,再回到5px

span {
animation: loading 0.6s 0s linear infinite alternate;
}

@keyframes loading {
0% {
width: 5px;
}
100% {
width: 48px;
}
}

效果图如下

【动画消消乐】HTML+CSS 自定义加载动画 070_css_03


注:span事先是设置固定在页面正中的,所以才会出现上图效果

步骤3

使用span的两个伪元素:span::before、span::after

同时设置为

  • 绝对定位( left: 0 top: 15px)
  • 宽度、高度均为5px
  • 背景色:白色
  • border-radius: 4px

span::before, span::after {
content: '';
width: 5px;
height: 5px;
border-radius: 4px;
background: white;
position: absolute;
left: 0;
top: 15px;
}

位置关系如下

【动画消消乐】HTML+CSS 自定义加载动画 070_html_04


为span::before、span::after添加同span一样的动画,只是动画开始延迟时间设置为0.6s

span::before, span::after {
animation: loading 0.6s 0.6s linear infinite alternate;
}

效果图如下:

【动画消消乐】HTML+CSS 自定义加载动画 070_学习笔记_05


注:可以发现,span和它的两个伪元素的左部分一直是处于同一条直线上

【动画消消乐】HTML+CSS 自定义加载动画 070_html_06

步骤4

分离span::before、span::after

将span::after移动至位于span上方15px处 且动画延迟时间修改为0.9s

span::after {
top: -15px;
animation-delay: 0.9s;
}

得到最终的效果图

【动画消消乐】HTML+CSS 自定义加载动画 070_学习笔记

结语

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

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

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


【动画消消乐】HTML+CSS 自定义加载动画 070_前端_08


举报

相关推荐

0 条评论