【動畫消消樂|CSS】079.單span標簽實現(xiàn)自定義簡易過渡動畫

前言

Hello!小伙伴!

非常感謝您閱讀海轟的文章,倘若文中有錯誤的地方,歡迎您指出~

?

自我介紹 ?(?ˊ?ˋ)?

昵稱:海轟

標簽:程序猿|C++選手|學(xué)生

簡介:因C語言結(jié)識編程,隨后轉(zhuǎn)入計算機專業(yè),有幸拿過國獎、省獎等,已保研。目前正在學(xué)習(xí)C++/Linux(真的真的太難了~)

學(xué)習(xí)經(jīng)驗:扎實基礎(chǔ) + 多做筆記 + 多敲代碼 + 多思考 + 學(xué)好英語!

?
<font color="red" font-wight="800">【動畫消消樂】</font> 平時學(xué)習(xí)生活比較枯燥,無意之間對一些網(wǎng)頁、應(yīng)用程序的過渡/加載動畫產(chǎn)生了濃厚的興趣,想知道具體是如何實現(xiàn)的? 便在空閑的時候?qū)W習(xí)下如何使用css實現(xiàn)一些簡單的動畫效果,文章僅供作為自己的學(xué)習(xí)筆記,記錄學(xué)習(xí)生活,爭取理解動畫的原理,多多“消滅”動畫!

效果展示

在這里插入圖片描述

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: 20px;
    height: 12px;
    display: inline-block;
    position: relative;
    border-radius: 4px;
    background: white;
    animation: loading 0.6s 0.3s ease infinite alternate;
}

span::before, span::after {
    content: '';
    width: 20px;
    height: 12px;
    background: white;
    position: absolute;
    border-radius: 4px;
    top: 0;
    right: 110%;
    animation: loading 0.6s ease infinite alternate;
}

span::after {
    left: 110%;
    right: auto;
    animation-delay: 0.6s;
}

@keyframes loading {
    0% {
        width: 20px;
    }
    100% {
        width: 48px;
    }
}

原理詳解

步驟1

使用一個span標簽

<span></span>

設(shè)置為

  • 寬度為20px 高度為12px
  • 相對定位
  • 背景顏色:白色
  • border-radius: 4px;
span {
    width: 20px;
    height: 12px;
    position: relative;
    border-radius: 4px;
    background: white;
}

效果圖如下:

在這里插入圖片描述

步驟2

使用span::before、span::after

同時設(shè)置為:

  • 寬度為20px 高度為12px
  • 背景色為白色
  • 絕對定位( top: 0; right: 110%;)
  • border-radius: 4px;
span::before, span::after {
    content: '';
    width: 20px;
    height: 12px;
    background: white;
    position: absolute;
    border-radius: 4px;
    top: 0;
    right: 110%;
}

效果圖如下:

在這里插入圖片描述

步驟3

再單獨設(shè)置span::after

使其與before分離 位于span右邊

  • 位置為:left: 110%;
  • 背景色為紅色(便于區(qū)分before)
span::after {
    left: 110%;
    background-color: red;
}

效果圖如下:

在這里插入圖片描述

步驟4

為span添加動畫

只需要設(shè)置 span寬度一個屬性變化 即可,效果描述為:

  • 初始位置:width: 20px;
  • 末尾位置:width: 48px;
@keyframes loading {
    0% {
        width: 20px;
    }
    100% {
        width: 48px;
    }
}

span使用該動畫

span {
    animation: loading 0.6s ease infinite alternate;
}

效果圖如下:

在這里插入圖片描述

步驟5

對span::before、span::after使用同樣的動畫

從位置關(guān)系上來說

從左到右依次是:

span::before、span、span::after


在這里插入圖片描述

為了使得動畫有一定的錯落感

分別設(shè)置動畫開始延遲時間為0s 0.3s 0.6s(與位置相對應(yīng))

span::before, span::after {
    animation: loading 0.6s ease infinite alternate;
}
span {
    animation: loading 0.6s 0.3s ease infinite alternate;
}

span::after {
    animation-delay: 0.6s;
}

效果如下:

在這里插入圖片描述

步驟6

最后再注釋掉span::after的背景顏色紅色即可

span::after {
    background-color: red;
}

得到最終的效果

在這里插入圖片描述

結(jié)語

文章僅作為學(xué)習(xí)筆記,記錄從0到1的一個過程

希望對您有所幫助,如有錯誤歡迎小伙伴指正~

我是 <font color="#0984e3">海轟?(?ˊ?ˋ)?</font>

如果您覺得寫得可以的話,請點個贊吧

謝謝支持??


在這里插入圖片描述
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容