Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
如何使用CSS实现旋转环绕动画_transform rotate结合translate_创想鸟

如何使用CSS实现旋转环绕动画_transform rotate结合translate

答案:通过嵌套结构与CSS transform 实现环绕动画。外层容器 .orbit-container 应用旋转动画 @keyframes orbit,使子元素 .orbiter-wrapper 以中心为原点转动;.orbiter-wrapper 使用 translate(-50%, -50%) 定位到容器中心,其子元素 .orbiter 再通过 translate(100px, 0) 偏移至轨道半径位置,从而在父容器旋转时沿圆形路径运动。关键在于 transform 执行顺序为从右到左,需确保平移在旋转前生效,最终实现子元素围绕中心点持续旋转的视觉效果。

如何使用css实现旋转环绕动画_transform rotate结合translate

要实现一个元素围绕另一个元素旋转的环绕动画,可以结合CSS的 transform 属性中的 rotate() 和 translate() 函数来完成。关键在于合理使用嵌套结构和坐标变换顺序。

基本原理说明

旋转环绕效果的本质是:让子元素先“平移”到父容器半径位置,再通过父容器整体旋转,带动子元素做圆周运动。由于CSS中 transform 的执行顺序是从右到左,所以组合使用时要注意函数顺序。

例如:

使用 rotate 控制旋转角度使用 translate 定位到环绕半径的位置通过 @keyframes 动画不断改变 rotate 值

HTML结构示例

需要一个外层容器作为旋转中心,内部包含中心点和环绕物:

立即学习“前端免费学习笔记(深入)”;

CSS实现代码

设置容器相对定位,环绕元素通过绝对定位+变换实现轨迹:

TextCortex TextCortex

AI写作能手,在几秒钟内创建内容。

TextCortex 62 查看详情 TextCortex

.orbit-container {  position: relative;  width: 200px;  height: 200px;  animation: orbit 4s linear infinite;}

.center-point {position: absolute;top: 50%;left: 50%;width: 20px;height: 20px;background-color: #333;border-radius: 50%;transform: translate(-50%, -50%);}

.orbiter {position: absolute;top: 50%;left: 50%;width: 10px;height: 10px;background-color: red;border-radius: 50%;transform: translate(100px, 0) translateY(-50%) rotate(0);}

@keyframes orbit {from { transform: rotate(0deg); }to { transform: rotate(360deg); }}

关键细节解析

注意 orbiter 的 transform 设置为:translate(100px, 0) + rotate() 实际无效,因为 rotate 是相对于自身原点。正确方式应由容器旋转带动:

将 translate 移出 orbiter,改为在容器上控制或改用嵌套结构更清晰

推荐改进方案:

.orbit-container {  position: relative;  width: 200px;  height: 200px;  animation: orbit 4s linear infinite;}

.orbiter-wrapper {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);}

.orbiter {width: 10px;height: 10px;background: blue;border-radius: 50%;transform: translate(100px, 0); / 先移动到右侧 /}

这样,.orbit-container 旋转时,.orbiter 就会以中心为圆心绕行。

基本上就这些,掌握 transform 执行顺序和坐标系统是实现这类动画的核心。

以上就是如何使用CSS实现旋转环绕动画_transform rotate结合translate的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/978668.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
咚奇刚新形象揭秘:美术总监说尖尖的头非常重要
上一篇 2025年12月1日 20:27:33
Java获取项目路径的简便方法
下一篇 2025年12月1日 20:27:37

相关推荐

发表回复

登录后才能评论
关注微信