如何用纯 CSS 创作一个沙漏 loader
效果预览
按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。
https://codepen.io/comehope/pen/VGegxr
可交互视频
此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。
请用 chrome, safari, edge 打开观看。
https://scrimba.com/p/pEgDAM/cVRr9cp
源代码下载
每日前端实战系列的全部源代码请从 github 下载:
https://github.com/comehope/front-end-daily-challenges
代码解读
定义 dom,容器中包含 2 个元素,分别代表沙漏的上半部和下半部:
<div class="loader"> <span class="top"></span> <span class="bottom"></span> </div>
居中显示:
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background-color: gainsboro; }
定义容器尺寸,并设置子元素整体布局:
.loader { width: 4.3em; height: 9.8em; font-size: 10px; position: relative; display: flex; flex-direction: column; align-items: center; justify-content: space-between; }
画出 2 个正方形:
.top, .bottom { width: 3.5em; height: 3.5em; border-style: solid; border-color: saddlebrown; }
通过边框、圆角和旋转,把 2 个正方形变成沙漏形状:
.top, .bottom { border-width: 0.2em 0.2em 0.6em 0.6em; border-radius: 50% 100% 50% 30%; } .top { transform: rotate(-45deg); } .bottom { transform: rotate(135deg); }
用伪元素画出沙子,上部的沙子的顶部是大圆弧,下部的沙子的顶部是小圆弧:
.top::before, .bottom::before { content: ''; position: absolute; width: inherit; height: inherit; background-color: deepskyblue; } .top::before { border-radius: 0 100% 0 0; } .bottom::before { border-radius: 0 0 0 35%; }
定义沙子的动画属性:
.top::before, .bottom::before { animation: 2s linear infinite; }
增加沙子从沙漏的上半部落下的动画效果:
.top::before { animation-name: drop-sand; } @keyframes drop-sand { to { transform: translate(-2.5em, 2.5em); } }
增加沙子的沙漏在下半部堆积的动画效果:
.bottom::before { transform: translate(2.5em, -2.5em); animation-name: fill-sand; } @keyframes fill-sand { to { transform: translate(0, 0); } }
隐藏沙漏上半部和下半部容器外的部分,此时上面 2 个动画的叠加效果是沙子从上半部漏下,慢慢在下半部堆积:
.top, .bottom { overflow: hidden; }
用外层容器的伪元素制作一个窄长条,模拟流动的沙子:
.loader::after { content: ''; position: absolute; width: 0.2em; height: 4.8em; background-color: deepskyblue; top: 1em; }
增加沙子流动的动画效果:
.loader::after { animation: flow 2s linear infinite; } @keyframes flow { 10%, 100% { transform: translateY(3.2em); } }
最后,增加沙漏的翻转动画:
.loader { animation: rotating 2s linear infinite; } @keyframes rotating { 0%, 90% { transform: rotate(0); } 100% { transform: rotate(0.5turn); } }
大功告成!
原文地址:https://segmentfault.com/a/1190000016153878
相关推荐
-
如何用纯 CSS 创作一种文字断开的交互特效 特效实现
2018-11-20
-
如何用纯 CSS 创作一个小球上台阶的动画 特效实现
2018-11-29
-
如何用纯 CSS 创作一个晃动的公告板 特效实现
2018-11-21
-
如何利用 CSS 的动画原理,创作一个乒乓球对打动画 特效实现
2018-11-21
-
如何不用 transition 和 animation 也能做网页动画 特效实现
2018-11-21
-
如何用纯 CSS 创作一个方块旋转动画 特效实现
2018-12-6
-
HTML5 之图片上传预处理 特效实现
2017-6-5
-
如何用纯 CSS 和 D3 创作一只扭动的蠕虫 特效实现
2018-12-7
-
如何用纯 CSS 创作六边形按钮特效 特效实现
2018-11-21
-
如何实现一个简单的雨滴动画?手把手告诉你 特效实现
2019-1-2