本篇文章给大家带来的内容是关于如何使用CSS和D3实现黑白交叠的动画效果,文章介绍了对js中this的理解,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
效果预览

代码解读
定义 dom,容器中包含 3 个子元素,每个子元素代表一个圆:
<div class="circles"> <span></span> <span></span> <span></span> </div>
居中显示:
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background-color: black; }
定义容器尺寸:
.circles { width: 60vmin; height: 60vmin; }
画出容器中的1个圆:
.circles { position: relative; } .circles span { position: absolute; box-sizing: border-box; width: 50%; height: 50%; background-color: white; border-radius: 50%; left: 25%; }
定义变量,画出多个圆,每个圆围绕着第 1 个圆的底部中点旋转,围成一个更大的圆形:
.circles { --particles: 3; } .circles span { transform-origin: bottom center; --deg: calc(360deg / var(--particles) * (var(--n) - 1)); transform: rotate(var(--deg)); } .circles span:nth-child(1) { --n: 1; } .circles span:nth-child(2) { --n: 2; } .circles span:nth-child(3) { --n: 3; }
为子元素增加动画效果:
.circles span { animation: rotating 5s ease-in-out infinite; } @keyframes rotating { 0% { transform: rotate(0); } 50% { transform: rotate(var(--deg)) translateY(0); } 100% { transform: rotate(var(--deg)) translateY(100%) scale(2); } }
设置子元素混色模式,使子元素间交叠的部分显示成黑色:
.circles span { mix-blend-mode: difference; }
为容器增加动画效果,抵销子元素放大,使动画流畅衔接:
.circles { animation: zoom 5s linear infinite; } @keyframes zoom { to { transform: scale(0.5) translateY(-50%); } }
接下来用 d3 批量处理 dom 元素和 css 变量。
引入 d3 库:
<script src="https://d3js.org/d3.v5.min.js"></script>
用 d3 为表示圆数量的变量赋值:
const COUNT_OF_PARTICLES = 30; d3.select('.circles') .style('--particles', COUNT_OF_PARTICLES)
用 d3 生成 dom 元素:
d3.select('.circles') .style('--particles', COUNT_OF_PARTICLES) .selectAll('span') .data(d3.range(COUNT_OF_PARTICLES)) .enter() .append('span');
用 d3 为表示子元素下标的变量赋值:
d3.select('.circles') .style('--particles', COUNT_OF_PARTICLES) .selectAll('span') .data(d3.range(COUNT_OF_PARTICLES)) .enter() .append('span') .style('--n', (d) => d + 1);
删除掉 html 文件中的相关 dom 元素和 css 文件中相关的 css 变量。
最后,把圆的数量调整为 30 个:
const COUNT_OF_PARTICLES = 30;
大功告成!
相关推荐:
如何使用CSS实现货车loader的效果以上是如何使用CSS和D3实现黑白交叠的动画效果的详细内容。更多信息请关注PHP中文网其他相关文章!

在本周的综述中:datepickers正在让键盘用户头痛,一个新的Web组件编译器,有助于与Fouc进行战斗,我们终于获得了造型列表项目标记,以及在您的网站上获得网络攻击的四个步骤。

在本周的Web平台新闻世界中,Google搜索控制台可以更轻松地查看爬行的标记,我们了解到自定义属性


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

Dreamweaver Mac版
视觉化网页开发工具

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

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

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