css php 源代码,如何使用CSS实现眼冒金星的动画效果(附源码)

本篇文章给大家带来的内容是关于如何使用CSS实现眼冒金星的动画效果(附源码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

效果预览

b44f55d0ad644be9cfa33ba4702b9e71.gif

源代码下载

https://github.com/comehope/front-end-daily-challenges

代码解读

定义 dom,容器中包含 9 个子元素:

居中显示:body {

margin: 0;

height: 100vh;

display: flex;

align-items: center;

justify-content: center;

background-color: black;

}

设置容器中子元素的布局方式,形成一个 3 * 3 的网格,其中 --columns 是网格每一边上的子元素数量:.container {

display: grid;

--columns: 3;

grid-template-columns: repeat(var(--columns), 1fr);

}

定义子元素样式:.container span {

width: 25px;

height: 25px;

color: lime;

background-color: currentColor;

}

增加子元素的动画效果,总动画时长是 5 秒,其中第 1 秒(0% ~ 20%)有动画,其余 4 秒(20% ~ 100%)静止:.container span {

transform: scale(0);

animation: spin 5s linear infinite;

}

@keyframes spin {

0% {

transform: rotate(0deg) scale(1);

}

5%, 15% {

transform: rotate(90deg) scale(0);

background: white;

}

17.5% {

transform: rotate(180deg) scale(1);

background-color: currentColor;

}

20%, 100% {

transform: rotate(90deg) scale(0);

}

}

设置动画延时,使各子元素的动画随机延时 4 秒之内的任意时间:.container span {

animation-delay: calc(var(--delay) * 1s);

}

.container span:nth-child(1) { --delay: 0.8 }

.container span:nth-child(2) { --delay: 0.2 }

.container span:nth-child(3) { --delay: 1.9 }

.container span:nth-child(4) { --delay: 3.9 }

.container span:nth-child(5) { --delay: 2.8 }

.container span:nth-child(6) { --delay: 3.5 }

.container span:nth-child(7) { --delay: 1.5 }

.container span:nth-child(8) { --delay: 2.3 }

.container span:nth-child(9) { --delay: 1.7 }

至此,静态效果完成,接下来批量处理 dom 元素。

引入 d3 库:

删除掉 css 文件中的 --columns 变量声明,用 d3 为变量赋值:const COLUMNS = 3;

d3.select('.container')

.style('--columns', COLUMNS);

删除掉 html 文件中的 子元素,用 d3 动态生成:d3.select('.container')

.style('--columns', COLUMNS)

.selectAll('span')

.data(d3.range(COLUMNS * COLUMNS))

.enter()

.append('span');

删除掉 css 文件中的 --delay 变量声明,用 d3 为变量生成随机数:d3.select('.container')

.style('--columns', COLUMNS)

.selectAll('span')

.data(d3.range(COLUMNS * COLUMNS))

.enter()

.append('span')

.style('--delay', () => Math.random() * 4);

最后,把边长改为 15,生成更多的子元素,加强视觉效果:const COLUMNS = 15;以上就是本篇文章的全部内容了,关于上述代码中所用到的css知识大家可以参考php中文网的css开发手册。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值