html怎么让方块自动旋转,如何使用纯CSS实现一个圆环旋转错觉的动画效果(附源码)...

5268f80b9b1e01f982625ef6fac83ca1.png

本篇文章给大家带来的内容是关于如何使用纯CSS实现一个圆环旋转错觉的动画效果,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

效果预览

d0a512405c5359ad9bf6d6ab2c8fd9c2.gif

源代码下载

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

代码解读

定义 dom,容器中包含 10 个

子元素,每个
子元素内还有一个 子元素:

定义容器尺寸:.container {

width: 17em;

height: 17em;

font-size: 16px;

}

定义子元素的尺寸,和容器相同:.container {

position: relative;

}

.container div {

position: absolute;

width: inherit;

height: inherit;

}

在子元素的正中画一个黄色的小方块:.container div {

display: flex;

align-items: center;

justify-content: center;

}

.container span {

position: absolute;

width: 1em;

height: 1em;

background-color: yellow;

}

增加让小方块左右移动的动画效果,动画时长还会在后面用到,所以定义成变量:.container span {

--duration: 2s;

animation: move var(--duration) infinite;

}

@keyframes move {

0%, 100% {

left: calc(10% - 0.5em);

}

50% {

left: calc(90% - 0.5em);

}

}

用贝赛尔曲线调整动画的时间函数,使小方块看起来就像在左右两侧跳来跳去:.container span {

animation: move var(--duration) cubic-bezier(0.6, -0.3, 0.7, 0) infinite;

}

增加小方块变形的动画,使它看起来有下蹲起跳的拟人效果:.container span {

animation:

move var(--duration) cubic-bezier(0.6, -0.3, 0.7, 0) infinite,

morph var(--duration) ease-in-out infinite;

}

@keyframes morph {

0%, 50%, 100% {

transform: scale(0.75, 1);

}

25%, 75% {

transform: scale(1.5, 0.5);

}

}

至此,完成了 1 个方块的动画。接下来设置多个方块的动画效果。

为子元素定义 CSS 下标变量:.container div:nth-child(1) { --n: 1; }

.container div:nth-child(2) { --n: 2; }

.container div:nth-child(3) { --n: 3; }

.container div:nth-child(4) { --n: 4; }

.container div:nth-child(5) { --n: 5; }

.container div:nth-child(6) { --n: 6; }

.container div:nth-child(7) { --n: 7; }

.container div:nth-child(8) { --n: 8; }

.container div:nth-child(9) { --n: 9; }

旋转子元素,使小方块分布均匀地在容器的四周,围合成一个圆形:.container p {

transform: rotate(calc(var(--n) * 40deg));

}

设置动画延时,现在看起来就像是一群小方块贴着一个圆的内边线在旋转了(但实际上没有任何元素在做旋转运动,大脑感觉到的旋转是一种错觉):.container span {

animation-delay: calc(var(--duration) / 9 * var(--n) * -1);

}

最后,为小方块上色:.container span {

background-color: hsl(calc(var(--n) * 80deg), 100%, 70%);

}

大功告成!想要了解更多css知识,可以去Gxl网css教程栏目去学习一下。

相关推荐:

如何使用纯CSS实现太阳和地球和月亮的运转模型动画

如何使用纯CSS实现一个足球场的俯视图(附源码)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值