CSS3通过var()和calc()函数实现动画特效
作者:互联网
2026-04-05

预习知识点.
- 动画帧
- 背景渐变
- var() 和 calc() 的使用
- flex布局的场景
Start:
创建HTML结构:
因为我们要转圈圈, 所以需要20个小盒子来组成我们的圈盒子,里面加上 style 样式: --i :num 这样我们获取到后面的数值.
盒子居中:
*{ margin: 0; padding: 0; box-sizing: border-box; } section{ display:flex; justify-content: center; align-items: center; min-height: 100vh; background: -webkit-linear-gradient(left top, pink, orange); }使用 flex 布局, 讲盒子定位到正中央的位置,background: -webkit-linear-gradient(left top, pink, orange);这个是渐变背景.
设置 loading 盒子大小.
.loading{ position: relative; width: 250px; height: 250px; }定位loading 盒子里面的文本和圈盒子.
.loading .text::after{ content: "Loading"; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); color: #000; font-size: 24px; font-weight:600; height: 66px; width: 130px; text-align: center; line-height: 66px; transition: all .5s; letter-spacing: 2px; }.loading .clock{ position:absolute; left: 50%; height: 25px; width: 8px; background-color:red; transform: rotate(calc(18deg * var(--i))); transform-origin: 0 125px; animation: clock 1.2s linear infinite; animation-delay: calc(0.06s * var(--i)); }通过 var (–i) 我们就可以获取到 该标签 style 里面 i 的num值,度数的计算 360 / 20 = 18 deg 因为我们是20个圈盒子, 每个旋转 18deg,之后的都叠加旋转, 就可以达到这个效果。 但是如果不更改旋转的位置, 那么就会绕着圈盒子的正中央直接进行旋转, 不会散开,而直接构成一个圆。

圈盒子的旋转定位 就是这样来的。
定义动画,添加动画
@keyframes clock { 0%, 50%{ background-color:pink; box-shadow: none; } 50.1%, 100%{ background-color: red; box-shadow: 0 0 5px red, 0 0 10px red, 0 0 25px red, 0 0 40px red; } }transform-origin: 0 125px; animation: clock 1.2s linear infinite; animation-delay: calc(0.06s * var(--i));
对应盒子阴影, 可以设置多个值, 这样更炫.
加上Hover事件 停止动画
loading .text:hover::after{ content: "Ended"; transform: translate(-50%, -50%) translateY(-8px) scale(1.3); color: red; } .loading:hover .clock{ animation-play-state: paused; }到此这篇关于CSS3通过var()和calc()函数实现动画特效的文章就介绍到这了,更多相关css动画效果内容请搜索本站以前的文章或继续浏览下面的相关文章,希望大家以后多多支持本站!
相关推荐
专题
+ 收藏
+ 收藏
+ 收藏
+ 收藏
+ 收藏
+ 收藏
最新数据
相关文章
Python 操作 Word 文档节与页面设置
Python后端开发准则
Python 面向对象编程
第2周 Day 4:英语 Agent Web 版上线:从命令行到浏览器
python从入门到精通-第8章: 类型系统 — Python的类型注解革命
基于 SelectDB 实现 Hive 数据湖统一分析:洋钱罐全球一体化探索分析平台升级实践
使用 Python 压缩 PDF 文件的大小
Envira Gallery v1.13.2 汉化版 WordPress高级图库插件
观测云数据转发和存档最佳实践
Python 错误和异常处理
AI精选
