我是靠谱客的博主 潇洒鸭子,这篇文章主要介绍中秋献礼,分享一个CSS日地月公转动画效果!,现在分享给大家,希望可以做个参考。

中秋节快到了,下面本篇文章给大家分享一个纯CSS实现的日地月公转动画效果,打开快来学习一下!

1.gif

为了这次掘金的中秋活动,我也算是苦思冥想了两天,终于想到了一个在掘金没见人做过的东西(应该没做过吧,我也不知道)—— 用 HTML+CSS 模拟日地月的公转。【相关推荐:《css视频教程》】

我们都知道中秋的月亮又大又圆,是因为太阳地球月亮在公转过程中处在了一条直线上,地球在中间,太阳和月球分别在地球的两端,这天的月相便是满月。这段可以略过,是为了跟中秋扯上关系。

但因为我根本没咋学过前端,这两天恶补了一下重学了 flexboxgrid ,成果应该说还挺好看(如果我的审美没有问题的话)。

配色我挺喜欢的,希望你也喜欢。

源码我放到了 CodePen 上,链接 Sun Earth Moon (codepen.io)

HTML

重点是CSS,HTML放上三个 div 就ok了。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title>Mancuoj</title> <link href="simulation.css" rel="stylesheet" /> </head> <body> <h1>Mancuoj</h1> <figure> <div></div> <div> <div></div> </div> </figure> </body> </html>
登录后复制

背景和文字

导入我最喜欢的 Lobster 字体,然后设为白色,字体细一点。

复制代码
1
2
3
4
5
6
7
8
@import url("https://fonts.googleapis.com/css2?family=Lobster&display=swap"); h1 { color: white; font-size: 60px; font-family: Lobster, monospace; font-weight: 100; }
登录后复制

背景随便找了一个偏黑紫色,然后把画的内容设置到中间。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
body { margin: 0; height: 100vh; display: flex; align-items: center; justify-content: center; background-color: #2f3141; } .container { font-size: 10px; width: 40em; height: 40em; position: relative; display: flex; align-items: center; justify-content: center; }
登录后复制

日地月动画

众所周知:地球绕着太阳转,月球绕着地球转。

我们画的是公转,太阳就直接画出来再加个阴影高光,月亮地球转就可以了。

最重要的其实是配色(文章末尾有推荐网站),我实验好长时间的配色,最终用了三个渐变色来表示日地月。

复制代码
1
2
3
日: linear-gradient(#fcd670, #f2784b); 地: linear-gradient(#19b5fe, #7befb2); 月: linear-gradient(#8d6e63, #ffe0b2);
登录后复制

CSS 应该难不到大家,随便看看吧。

轨道用到了 border,用银色线条当作公转的轨迹。

动画用到了自带的 animation ,每次旋转一周。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
.sun { position: absolute; width: 10em; height: 10em; background: linear-gradient(#fcd670, #f2784b); border-radius: 50%; box-shadow: 0 0 8px 8px rgba(242, 120, 75, 0.2); } .earth { --diameter: 30; --duration: 36.5; } .moon { --diameter: 8; --duration: 2.7; top: 0.3em; right: 0.3em; } .earth, .moon { position: absolute; width: calc(var(--diameter) * 1em); height: calc(var(--diameter) * 1em); border-width: 0.1em; border-style: solid solid none none; border-color: silver transparent transparent transparent; border-radius: 50%; animation: orbit linear infinite; animation-duration: calc(var(--duration) * 1s); } @keyframes orbit { to { transform: rotate(1turn); } } .earth::before { --diameter: 3; --color: linear-gradient(#19b5fe, #7befb2); --top: 2.8; --right: 2.8; } .moon::before { --diameter: 1.2; --color: linear-gradient(#8d6e63, #ffe0b2); --top: 0.8; --right: 0.2; } .earth::before, .moon::before { content: ""; position: absolute; width: calc(var(--diameter) * 1em); height: calc(var(--diameter) * 1em); background: var(--color); border-radius: 50%; top: calc(var(--top) * 1em); right: calc(var(--right) * 1em); }
登录后复制

总结

参加个活动真不容易,不过前端还是挺好玩的。

推荐几个我找颜色的网站吧:

  • 免费的渐变背景CSS3样式 | oulu.me

  • uiGradients - Beautiful colored gradients

  • Color Palettes for Designers and Artists - Color Hunt

  • 中国色 - 中国传统颜色 (zhongguose.com)

  • Palettes | Flat UI Colors 280 handpicked colors ready for COPY & PASTE

  • Material Design Colors, Color Palette | Material UI

最后

以上就是潇洒鸭子最近收集整理的关于中秋献礼,分享一个CSS日地月公转动画效果!的全部内容,更多相关中秋献礼内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(103)

评论列表共有 0 条评论

立即
投稿
返回
顶部