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

前言

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

效果展現

在這裏插入圖片描述

思路

這裏用span元素表明外層白色圓圈html

兩個紅色小球分別用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>

CSSflex

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;

效果圖以下
在這裏插入圖片描述ui

步驟2

span::before和span::after設置spa

  • 寬度、高度均爲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;

效果圖以下3d

在這裏插入圖片描述

步驟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的一個過程。但願對您有所幫助,若有錯誤歡迎小夥伴指正~

我是海轟ଘ(੭ˊᵕˋ)੭,若是您以爲寫得能夠的話,請點個贊吧

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

謝謝支持❤️

在這裏插入圖片描述

相關文章
相關標籤/搜索