0
点赞
收藏
分享

微信扫一扫

【动画消消乐|CSS】087.HTML+CSS实现自定义简易过渡动画

拾光的Shelly 2021-09-25 阅读 38

前言

效果展示

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;
  background: rgba(255, 255, 255, 0.15);
  position: relative;
  overflow: hidden;
}

span::after {
  content: '';
  width: 0%;
  height: 10px;
  background-color: white;
  background-image: linear-gradient(45deg, rgba(0, 0, 0, 0.25) 25%, transparent 25%, transparent 50%, rgba(0, 0, 0, 0.25) 50%, rgba(0, 0, 0, 0.25) 75%, transparent 75%, transparent);
  background-size: 15px 15px;
  position: absolute;
  top: 0;
  left: 0;
  animation: loading 4s ease-in infinite;
}

@keyframes loading {
  0% {
    width: 0
  }
  100% {
    width: 100%
  }
}

原理详解

步骤1

使用一个span标签

<span></span>

设置为

  • 宽度为100%
  • 高度为10px
  • 相对定位
  • 背景色:白色 透明级别:0.15
  span {
    width: 100%;
    height: 10px;
    background: rgba(255, 255, 255, 0.15);
    position: relative;
  }

效果如下图:

步骤2

利用span::after

设置为

  • 宽度10%
  • 高度10px
  • 绝对定位( top: 0; left: 0;)
  • 背景色:白色
  span::after {
    content: '';
    width: 10%;
    height: 10px;
    background-color: white;
    position: absolute;
    top: 0;
    left: 0;
  }

效果如下图:

步骤3

为span::after设置一个渐变背景颜色

  span::after {
    background-image: linear-gradient(45deg, 
    rgba(0, 0, 0, 0.25) 25%, 
    transparent 25%, 
    transparent 50%, 
    rgba(0, 0, 0, 0.25) 50%,
     rgba(0, 0, 0, 0.25) 75%, 
     transparent 75%, 
     transparent);
  }

效果如下图:


备注:

background-image: linear-gradient(45deg, rgba(0, 0, 0, 0.25) 25%, transparent 25%, transparent 50%, rgba(0, 0, 0, 0.25) 50%, rgba(0, 0, 0, 0.25) 75%, transparent 75%, transparent);可以理解为:

  • 45deg:渐变角度
  • 0% - rgba(0, 0, 0, 0.25) 25% : 0% - 25%都是灰色(0%初始位置代码中省略了)
  • transparent 25% - transparent 50%:25%-50%都是透明(显示span::after的原背景色:白色)
  • rgba(0, 0, 0, 0.25) 50% - rgba(0, 0, 0, 0.25) 75%, :50% - 75% 都是灰色
  • transparent 75% - transparent:75% - 100%为透明色

设置background-size: 15px 15px;

  span::after {
background-size: 15px 15px; 
  }

效果如下图:


备注:这里海轰也不太清楚为什么会这样变化???

步骤4

为span::after 添加动画

效果很简单 span::after宽度从0%到100%即可

  span::after {
    animation: loading 4s ease-in infinite;
  }
  
  @keyframes loading {
    0% {
      width: 0
    }
    100% {
      width: 100%
    }
  }

得到最终效果图

结语

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

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

我是 <font color="#0984e3">海轰ଘ(੭ˊᵕˋ)੭</font>

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

谢谢支持❤️


举报

相关推荐

0 条评论