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

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: #263238;
}
section {
width: 650px;
height: 300px;
padding: 10px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
border: 2px solid red;
}
span {
width: 36px;
height: 36px;
border-radius: 50%;
position: relative;
color: #fff;
animation: animloader 1s linear infinite alternate;
}
@keyframes animloader {
0% {
box-shadow: -110px -6px, -38px 6px, 38px -6px
}
33% {
box-shadow: -110px 6px, -38px -6px, 38px 6px
}
66% {
box-shadow: -110px -6px, -38px 6px, 38px -6px
}
100% {
box-shadow: -110px 6px, -38px -6px, 38px 6px
}
}
這個版本的代碼是仿照大佬的scss代碼用css復寫了一遍
其主要原理就是三個小球都是利用box-shadow陰影生成
然后利用影片
設定小球在每一個關鍵時刻的位置
平滑過渡產生影片
從而達到上圖的效果
但是如果顯示出原span標簽

就會發現
三個小球是沒有處于正中間的
而是向左產生了一點偏移
于是為了得到對稱的影片
海轟依據自己思路重新寫了一個影片,如下:
效果展示

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 class="a"></span>
<span class="b"></span>
<span class="c"></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 : 36px;
height: 36px;
border-radius: 50%;
position: relative;
background-color:white;
}
.a {
margin-right: 12px;
animation: animloader1 1s linear infinite alternate;
}
.b{
animation: animloader2 1s linear infinite alternate;
}
.c{
margin-left: 12px;
animation: animloader1 1s linear infinite alternate;
}
@keyframes animloader1
{
0%{
margin-top: -12px;
}
33%{
margin-top: 12px;
}
66%{
margin-top: -12px;
}
100%{
margin-top: 12px;
}
}
@keyframes animloader2 {
0%{
margin-top: 12px;
}
33%{
margin-top: -12px;
}
66%{
margin-top: 12px;
}
100%{
margin-top: -12px;
}
}
原理詳解
步驟1
這里使用了三個span標簽
分別代表三個白色小球
設定為:
- 寬度、高度均為36px
- 圓角化(50%)
- 背景色:白色
- 三個小球同行并列
span{
width : 36px;
height: 36px;
border-radius: 50%;
position: relative;
background-color:white;
}
效果圖如下

步驟2
增加左/右小球與中間小球的距離
/*左小球*/
.a {
margin-right: 12px;
}
/*中間小球*/
.b{
}
/*右小球*/
.c{
margin-left: 12px;
}
效果圖如下

步驟3
為左小球、右小球添加影片
效果描述:上下規律運動
這里海轟沒有使用陰影
使用的是利用margin-top來實作小球的上下運動
關鍵有2幀
這里以左小球為例,為了更好觀察,這里用紅色標記左小球
小球處于最高點margin-top: -12px;時

小球處于最低點margin-top: 12px;時

根據需求撰寫代碼如下
animation: animloader1 1s linear infinite alternate;
@keyframes animloader1
{
0%{
margin-top: -12px;
}
33%{
margin-top: 12px;
}
66%{
margin-top: -12px;
}
100%{
margin-top: 12px;
}
}
效果圖如下

步驟4
為中間小球添加影片
效果描述:上下規律運動
原理同左小球影片一樣
animation: animloader2 1s linear infinite alternate;
@keyframes animloader2 {
0%{
margin-top: 12px;
}
33%{
margin-top: -12px;
}
66%{
margin-top: 12px;
}
100%{
margin-top: -12px;
}
}
效果圖如下

步驟5
步驟3、4產生的影片疊加效果即為最終效果
注意:中間小球的起始、最終位置與左右小球相反即可

結語
學習來源:
https://codepen.io/bhadupranjal/pen/vYLZYqQ
文章僅作為學習筆記,記錄從0到1的一個程序,希望對您有所幫助,如有錯誤歡迎小伙伴指正~
我是海轟?(?ˊ?ˋ)?,如果您覺得寫得可以的話,請點個贊吧
寫作不易,「點贊」+「收藏」+「轉發」
謝謝支持??

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/282313.html
標籤:其他
