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

效果展示

在这里插入图片描述

Demo代码

HTML




    
    
    
    
    Document


    

CSS

html, body {
  margin: 0;
  height: 100%;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  background: #263238;
}

section {
  width: 650px;
  height: 300px;
  padding: 10px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  /* 红色边框仅作提示 */
  border: 2px solid red;
}

span {
  width: 96px;
  height: 96px;
  position: relative;
  animation: rotation 2s linear infinite;
}

span::before, span::after {
  position: absolute;
  content: '';
  width: 24px;
  height: 24px;
  border-radius: 50%;
}

span::before {
  top: 0;
  left: 0;
  background-color: white;
}

span::after {
  bottom: 0;
  right: 0;
  background-color: red;
}

@keyframes rotation {
  0% {
    transform: rotate(0deg)
  }
  100% {
    transform: rotate(360deg)
  }
}

原理详解

步骤1

使用span标签,设置

  • 宽度、高度均为96px
  • 相对定位
  • 背景色:yellow(这里只用作提示,后面会取消背景色的)
width: 96px;
  height: 96px;
  position: relative;
  background-color: yellow;

效果图如下

在这里插入图片描述

步骤2

使用span::before、span::after伪类元素,设置

  • 宽度、高度均为24px
  • 绝对定位

span::before

  • 颜色:白色
  • 位置:左上角

span::after

  • 颜色:红色
  • 位置:右下角
span::before, span::after {
  position: absolute;
  content: ''; 
  width: 24px;
  height: 24px;
}

span::before {
  top: 0;
  left: 0;
  background-color: white;
}

span::after {
  bottom: 0;
  right: 0;
  background-color: red;
}

效果图如下

在这里插入图片描述

步骤3

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

border-radius: 50%;

效果图如下

在这里插入图片描述

步骤4

取消span的背景色

效果图如下

在这里插入图片描述

步骤5

为span添加动画

  • 顺时针旋转(0-360度) 2s 无限循环
animation: rotation 2s linear infinite;

@keyframes rotation {
  0% {
    transform: rotate(0deg)
  }
  100% {
    transform: rotate(360deg)
  }
}

效果图如下

在这里插入图片描述

你可能感兴趣的:(「HTML+CSS」--自定义加载动画【039】)