这篇文章给大家介绍的内容是关于如何使用CSS实现变色旋转动画的动态效果,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
效果预览
代码解读
定义 dom,容器中包含 9 个元素:
<div class="container"> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> <span></span> </div>
居中显示:
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background-color: black; }
定义容器尺寸:
.container { width: 30em; height: 30em; font-size: 12px; }
设置容器中线条的样式:
.container { color: lime; } .container span { position: absolute; width: 5em; height: 5em; border-style: solid; border-width: 1em 1em 0 0; border-color: currentColor transparent; border-radius: 50%; }
让线条在容器中居中显示:
.container { display: flex; align-items: center; justify-content: center; }
定义变量,使线条从中心向外侧逐渐延伸:
.container span { --diameter: calc(5em + (var(--n) - 1) * 3em); width: var(--diameter); height: var(--diameter); } .container span:nth-child(1) { --n: 1; } .container span:nth-child(2) { --n: 2; } .container span:nth-child(3) { --n: 3; } .container span:nth-child(4) { --n: 4; } .container span:nth-child(5) { --n: 5; } .container span:nth-child(6) { --n: 6; } .container span:nth-child(7) { --n: 7; } .container span:nth-child(8) { --n: 8; } .container span:nth-child(9) { --n: 9; }
设置让线条旋转的动画效果:
.container span { animation: rotating linear infinite; animation-duration: calc(5s / (9 - var(--n) + 1)); } @keyframes rotating { to { transform: rotate(1turn); } }
定义改变颜色的动画效果,以色相环一周 360 度为 100%,--percent 变量是指位于这个 100% 的哪个位置:
@keyframes change-color { 0%, 100% { --percent: 0; } 10% { --percent: 10; } 20% { --percent: 20; } 30% { --percent: 30; } 40% { --percent: 40; } 50% { --percent: 50; } 60% { --percent: 60; } 70% { --percent: 70; } 80% { --percent: 80; } 90% { --percent: 90; } }
最后,把改变颜色的动画效果应用到容器上:
.container { --deg: calc(var(--percent) / 100 * 360deg); color: hsl(var(--deg), 100%, 50%); animation: change-color 5s linear infinite; }
大功告成!
相关文章推荐:
以上是如何使用CSS实现变色旋转动画的动态效果的详细内容。更多信息请关注PHP中文网其他相关文章!

文章讨论了CSS FlexBox,这是一种布局方法,用于有效地对齐和分布响应设计中的空间。它说明了FlexBox用法,将其与CSS网格进行了比较,并详细浏览了浏览器支持。

本文讨论了使用CSS创建响应网站的技术,包括视口元标签,灵活的网格,流体媒体,媒体查询和相对单元。它还涵盖了使用CSS网格和Flexbox一起使用,并推荐CSS框架

本文讨论了CSS盒装属性,该属性控制了元素维度的计算方式。它解释了诸如Content-Box,Border-Box和Padding-Box之类的值,以及它们对布局设计和形式对齐的影响。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

Atom编辑器mac版下载
最流行的的开源编辑器

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3 英文版
推荐:为Win版本,支持代码提示!