
純前端 - 原生自定義加載動畫
发布日期:2021-05-04 19:51:27
浏览次数:16
分类:精选文章
本文共 1918 字,大约阅读时间需要 6 分钟。
純前端 - 原生自定義加載動畫
參考
參考 | 鏈接 |
---|---|
「HTML+CSS」–自定义加载动画【007】 | https://cloud.tencent.com/developer/article/1811548 |
CSS3 animation(动画) 属性 | https://www.runoob.com/cssref/css3-pr-animation.html |
核心原理
其實這個功能相對很簡單,只是看到好玩想說自己實踐下順扁做個紀錄而已。其實原理就只是通過 span
和 span::after
以及簡單的 css3 動畫而已,很間單的。下面一步步來看看怎麼做的!
html 代碼
Document
css 代碼
/* loading.css */html, body { margin: 0; height: 100%;}body { display: flex; justify-content: center; align-items: center; background: #263238;}span { width: 120px; height: 120px; border: 10px solid transparent; display: flex; align-items: center; justify-content: center; position: relative; border-bottom-color: orange; border-radius: 50%; animation: loading 2s linear infinite;}span::after { position: relative; content: ""; display: inline-block; width: 96px; height: 96px; border: 10px solid #fff; border-radius: 50%;}@keyframes loading { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); }}
下面拆解下怎麼寫出上面代碼的。
Step1
首先先把 span
設置大小,弄出一個邊框來。
span { width: 120px; height: 120px; border: 10px solid #fff;}
Step2
基於 Step1 的外框在裡面弄出一個內框,與外框要留有一些間隙。這邊就使用到 css 的偽元素 ::
。
span { width: 120px; height: 120px; border: 10px solid #fff; display: flex; align-items: center; justify-content: center; position: relative;}span::after { position: relative; content: ""; display: inline-block; width: 96px; height: 96px; border: 10px solid #fff;}
Step3
這一步要把 1/4 的外框變成有色的。
border-bottom-color: orange;
Step4
然後把內框和外框都做圓角設置。
border-radius: 50%;
Step5
把外框樣式變得透明。
border: 10px solid transparent;
span
上這麼設置了 border
,但因為 border-bottom-color
設置了顏色所以依然會顯示。 Step6
最後就是添加動畫,這邊使用到了 css3 的 animation
屬性,配合 @keyframes
。
animation: loading 2s linear infinite;@keyframes loading { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); }}
到這邊就都大功搞成啦~最後來看看最終的效果圖!
效果展示
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2025年04月14日 18时43分38秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Hibernate(十四)抓取策略
2019-03-06
[菜鸟的设计模式之旅]观察者模式
2019-03-06
Spring-继承JdbcDaoSupport类后简化配置文件内容
2019-03-06
Java基础IO流(一)
2019-03-06
Hibernate入门(四)---------一级缓存
2019-03-06
MySQL事务(学习笔记)
2019-03-06
一个web前端开发者的日常唠叨
2019-03-06
内存分配-slab分配器
2019-03-06
技术写作技巧分享:我是如何从写作小白成长为多平台优秀作者的?
2019-03-06
Jupyter Notebook 暗色自定义主题
2019-03-06
[Python学习笔记]组织文件
2019-03-06
基于Redo Log和Undo Log的MySQL崩溃恢复流程
2019-03-06
从RocketMQ的Broker源码层面验证一下这两个点
2019-03-06
如何正确的在项目中接入微信JS-SDK
2019-03-06
纵览全局的框框——智慧搜索
2019-03-06
快服务流量之争:如何在快服务中占领一席之地
2019-03-06
【活动】直播揭秘<如何从0开发HarmonyOS硬件>
2019-03-06
Unity平台 | 快速集成华为性能管理服务
2019-03-06
详细实例教程!集成华为虚假用户检测,防范虚假恶意流量
2019-03-06
对模拟器虚假设备识别能力提升15%!每日清理大师App集成系统完整性检测
2019-03-06