效果预览

在线演示

按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。

https://codepen.io/comehope/pen/bMvbRp

可交互视频教程

此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。

请用 chrome, safari, edge 打开观看。

https://scrimba.com/p/pEgDAM/cp2dZcQ

源代码下载

本地下载

请从 github 下载。

https://github.com/comehope/front-end-daily-challenges/tree/master/017-swapping-colors-loader-animation

代码解读

定义 dom,一个容器中包含一个 span:

<div class="loader">
<span></span>
</div>

居中显示:

html,
body,
.loader {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background-color: black;
}

设置 span 的样式:

.loader {
width: 10em;
height: 10em;
font-size: 28px;
position: relative;
} .loader span {
position: absolute;
width: 100%;
height: 100%;
background-color: rgba(100%, 0%, 0%, 0.3);
box-sizing: border-box;
border: 0.5em solid;
border-color: white rgba(100%, 100%, 100%, 0.2);
}

在 dom 中把 span 增加到 5 个:

<div class="loader">
<span></span>
<span></span>
<span></span>
<span></span>
<span></span>
</div>

分别设置 5 个 span 的尺寸:

.loader span:nth-child(1) {
width: calc(20% + 20% * (5 - 1));
height: calc(20% + 20% * (5 - 1));
} .loader span:nth-child(2) {
width: calc(20% + 20% * (5 - 2));
height: calc(20% + 20% * (5 - 2));
} .loader span:nth-child(3) {
width: calc(20% + 20% * (5 - 3));
height: calc(20% + 20% * (5 - 3));
} .loader span:nth-child(4) {
width: calc(20% + 20% * (5 - 4));
height: calc(20% + 20% * (5 - 4));
} .loader span:nth-child(5) {
width: calc(20% + 20% * (5 - 5));
height: calc(20% + 20% * (5 - 5));
}

增加颜色变幻的动画效果:

.loader span {
animation: animate 5s ease-in-out infinite alternate;
} @keyframes animate {
0% {
/* red */
background-color: rgba(100%, 0%, 0%, 0.3);
} 25% {
/* yellow */
background-color: rgba(100%, 100%, 0%, 0.3);
} 50% {
/* green */
background-color: rgba(0%, 100%, 0%, 0.3);
} 75% {
/* blue */
background-color: rgba(0%, 0%, 100%, 0.3);
} 100% {
/* purple */
background-color: rgba(100%, 0%, 100%, 0.3);
}
}

再增加旋转效果:

@keyframes animate {
0% {
transform: rotate(0deg);
} 100% {
transform: rotate(720deg);
}
}

最后,为每个 span 设置动画延时,增加动感:

.loader span:nth-child(1) {
animation-delay: calc(0.2s * (5 - 1));
} .loader span:nth-child(2) {
animation-delay: calc(0.2s * (5 - 2));
} .loader span:nth-child(3) {
animation-delay: calc(0.2s * (5 - 3));
} .loader span:nth-child(4) {
animation-delay: calc(0.2s * (5 - 4));
} .loader span:nth-child(5) {
animation-delay: calc(0.2s * (5 - 5));
}

知识点

原文地址:https://segmentfault.com/a/1190000014807564

如何用纯 CSS 创作炫酷的同心矩形旋转动画的更多相关文章

  1. 前端每日实战:17# 视频演示如何用纯 CSS 创作炫酷的同心矩形旋转动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/bMvbRp 可交互视频教程 此视频 ...

  2. 17.纯 CSS 创作炫酷的同心矩形旋转动画

    原文地址:https://segmentfault.com/a/1190000014807564 感想: 这个特效不难,但是这想法可能想不到,哈哈,怎么又废了. HTML代码: <div cla ...

  3. 17.1拓展之纯 CSS 创作炫酷的同心圆旋转动画

    效果地址:https://codepen.io/flyingliao/pen/ebjEMm?editors=1100 HTML代码: <div class="loader"& ...

  4. 如何用纯 CSS 创作一个文本淡入淡出的 loader 动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/ERwpeG 可交互视频 ...

  5. 前端每日实战:53# 视频演示如何用纯 CSS 创作一个文本淡入淡出的 loader 动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/ERwpeG 可交互视频 此视频是可 ...

  6. 前端每日实战:160# 视频演示如何用纯 CSS 创作一个打开内容弹窗的交互动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/GYXvez 可交互视频 此视频是可 ...

  7. 前端每日实战:149# 视频演示如何用纯 CSS 创作一个宝路薄荷糖的动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/oagrvz 可交互视频 此视频是可 ...

  8. 前端每日实战:52# 视频演示如何用纯 CSS 创作一个小球绕着圆环盘旋的动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/gKxyWo 可交互视频 此视频是可 ...

  9. 如何用纯 CSS 创作文本滑动特效的 UI 界面

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/QrxxaW 可交互视频教 ...

随机推荐

  1. bzoj 1396: 识别子串【SAM+线段树】

    建个SAM,符合要求的串显然是|right|==1的节点多代表的串,设si[i]为right集合大小,p[i]为right最大的r点,这些都可以建出SAM后再parent树上求得 然后对弈si[i]= ...

  2. bzoj 1879: [Sdoi2009]Bill的挑战【状压dp】

    石乐志写容斥--其实状压dp就行 设f[i][s]表示前i个字母,匹配状态为s,预处理g[i][j]为第i个字母是j的1~n的集合,转移的时候枚举26个字母转移,最后答案加上正好有k个的方案即可 #i ...

  3. python 中site-packages 和 dist-packages的区别

    dist-packages is a Debian-specific convention that is also present in its derivatives, like Ubuntu. ...

  4. mysql 用 group by 和 order by同时使用

    首先,这是不可能实现的 mysql的查询的顺序 select -> from-> where->group by->having->order by. 但mysql的解析 ...

  5. HDU 6183 Color it(动态开点线段树)

    题目原网址:http://acm.hdu.edu.cn/showproblem.php?pid=6183 题目中文翻译: Time Limit: 20000/10000 MS (Java/Others ...

  6. Contextual Action bar(2) 简介,启动,各函数介绍

    一.Context Action Bar简介 它是一个ActionBar,有各种操作项,但它不是始终显示的ActionBar,它需要上下文才显示.样式如下: 二.Context Action Bar的 ...

  7. 小程序java解密

    <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk16< ...

  8. 关于IE 对 $.get 缓存的记录

    最近在IE9中碰到一个问题是, 当我对某个角色进行修改的时候,再点击查询还是修改之前的内容,但是实际数据库已经修改成功,纠结了好一会儿之后,才发现是 $.get请求的问题. 因为  IE对get请求, ...

  9. 复习-PEP8规范(转)

    PEP8 Python 编码规范 一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换 ...

  10. NSString 与NSMutableString的区别

      NSString 与NSMutableString的区别    Suppose You have a code like this NSString *s = [[NSString alloc]  ...