「HTML+CSS」--自定義加載動畫【010】


前言

Hello!小伙伴!
首先非常感謝您閱讀海轟的文章,倘若文中有錯誤的地方,歡迎您指出~
哈哈 自我介紹一下
昵稱:海轟
標簽:程序猿一只|C++選手|學生
簡介:因C語言結識編程,隨后轉入計算機專業,有幸拿過國獎、省獎等,已保研。目前正在學習C++/Linux(真的真的太難了~)
學習經驗:扎實基礎 + 多做筆記 + 多敲代碼 + 多思考 + 學好英語!
日常分享:微信公眾號【海轟Pro】記錄生活、學習點滴,分享一些源代碼或者學習資料,歡迎關注~

效果展示

在這里插入圖片描述

思路

這里用span元素代表外層白色圓圈

兩個紅色小球分別用span的兩個偽類::before和::after代表

根據效果圖,可以大概得出思路

  • 先利用span生成一個正方形,設置好邊框
  • 兩個偽類元素為絕對定位,分別位於正方形的左上和右下
  • 然后分別對其進行圓角處理
  • 最后添加旋轉動畫即可

Demo代碼

HTML

<!DOCTYPE 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: #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;
  border: 10px solid white;
  border-radius: 50%; 
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  animation: rotation 2s linear infinite;
}
span::before{
  position: absolute;
  content: '';
  top: 15px;
  left: 15px;
  width:  20px;
  height:  20px;
  background: red;
  border-radius: 50%;
}
span::after{
  position: absolute;
  content: '';
  bottom: 15px;
  right: 15px;
  width:  20px;
  height:  20px;
  background: red;
  border-radius: 50%;
}
@keyframes rotation {
  0% { transform: rotate(0deg) }
  100% { transform: rotate(360deg)
  }
}

原理詳解

步驟1

將span元素設置為

  • 一個96✖️96px的正方形
  • 邊框為10px,白色,solid
    width : 96px;
    height: 96px;
    border: 10px solid #fff;

效果圖如下
在這里插入圖片描述

步驟2

span::before和span::after設置

  • 寬度、高度均為20px
  • 絕對定位,其中before位於左上,after位於右下
  • 背景色為紅色
/* before的設置*/
position: absolute;
  content: '';
  top: 0;
  left: 0;
  width:  20px;
  height:  20px;
  background: red;
  
  /*after的設置*/
  position: absolute;
  content: '';
  bottom: 0;
  right: 0;
  width:  20px;
  height:  20px;
  background: red;

效果圖如下

在這里插入圖片描述

步驟3

稍微向正方形中心移動下::before和::after

/* before的設置*/
  top: 15px;
  left: 15px;
 
  /*after的設置*/
  bottom: 15px;
  right: 15px;

效果圖如下

在這里插入圖片描述

步驟4

對span、span::before、span::after設置圓角

border-radius: 50%;

效果圖如下
在這里插入圖片描述

步驟7

為span添加動畫

animation: rotation 1s linear infinite;
/*動畫實現*/
  @keyframes rotation {
    0% { transform: rotate(0deg) }
    100% { transform: rotate(360deg)
    }

效果圖如下

在這里插入圖片描述

結語

學習來源:

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

文章僅作為學習筆記,記錄從0到1的一個過程。希望對您有所幫助,如有錯誤歡迎小伙伴指正~

我是海轟ଘ(੭ˊᵕˋ)੭,如果您覺得寫得可以的話,請點個贊吧

寫作不易,「點贊」+「收藏」+「轉發」

謝謝支持❤️

在這里插入圖片描述
我的博客即將同步至騰訊雲+社區,邀請大家一同入駐:https://cloud.tencent.com/developer/support-plan?invite_code=hclw6x6lvkci


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM