摘要:源代码下载每日前端实战系列的全部源代码请从下载代码解读定义,容器中包含个元素居中显示定义容器尺寸画出太阳画出地球和月球的轨迹用伪元素画出地球和月球设置运转动画效果最后,隐藏可能会出现在容器外的部分大功告成
效果预览
按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。
https://codepen.io/comehope/pen/RJjQYY
可交互视频此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。
请用 chrome, safari, edge 打开观看。
https://scrimba.com/p/pEgDAM/cLQLyTR
源代码下载每日前端实战系列的全部源代码请从 github 下载:
https://github.com/comehope/front-end-daily-challenges
代码解读定义dom,容器中包含 3 个元素:
居中显示:
body {
margin: 0;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background-color: black;
}
定义容器尺寸:
.container {
font-size: 10px;
width: 40em;
height: 40em;
position: relative;
}
画出太阳:
.sun {
position: absolute;
top: 15em;
left: 15em;
width: 10em;
height: 10em;
background-color: yellow;
border-radius: 50%;
box-shadow: 0 0 3em white;
}
画出地球和月球的轨迹:
.earth,
.moon {
position: absolute;
border-style: solid;
border-color: white transparent transparent transparent;
border-width: 0.1em 0.1em 0 0;
border-radius: 50%;
}
.earth {
top: 5em;
left: 5em;
width: 30em;
height: 30em;
}
.moon {
top:0;
right: 0;
width: 8em;
height: 8em;
}
用伪元素画出地球和月球:
.earth::before,
.moon::before {
position: absolute;
border-radius: 50% ;
content: "";
}
.earth::before {
top: 2.8em;
right: 2.5em;
height: 3em;
width: 3em;
background-color: aqua;
}
.moon::before {
top: 0.8em;
right: 0.2em;
width: 1.2em;
height: 1.2em;
background-color: silver;
}
设置运转动画效果:
/* rotation period 365.2422 days */
.earth {
animation: orbit 36.5s linear infinite;
}
/* rotation period 27.322 days */
.moon {
animation: orbit 2.7s linear infinite;
}
@keyframes orbit {
to {
transform: rotate(360deg);
}
}
最后,隐藏可能会出现在容器外的部分:
body {
overflow: hidden;
}
大功告成!
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/116728.html
摘要:源代码下载每日前端实战系列的全部源代码请从下载代码解读定义,容器中包含个元素居中显示定义容器尺寸画出太阳画出地球和月球的轨迹用伪元素画出地球和月球设置运转动画效果最后,隐藏可能会出现在容器外的部分大功告成 showImg(https://segmentfault.com/img/bVbcpRv?w=500&h=500); 效果预览 按下右侧的点击预览按钮可以在当前页面预览,点击链接可以...
摘要:过往项目年月份项目汇总共个项目年月份项目汇总共个项目年月份发布的项目前端每日实战专栏每天分解一个前端项目,用视频记录编码过程,再配合详细的代码解读,是学习前端开发的活的参考书视频演示如何用纯创作一个表达怀念童年心情的条纹彩虹心特效视频演示如 过往项目 2018 年 5 月份项目汇总(共 30 个项目) 2018 年 4 月份项目汇总(共 8 个项目) 2018 年 6 月份发布的项目 ...
摘要:过往项目年月份项目汇总共个项目年月份项目汇总共个项目年月份发布的项目前端每日实战专栏每天分解一个前端项目,用视频记录编码过程,再配合详细的代码解读,是学习前端开发的活的参考书视频演示如何用纯创作一个表达怀念童年心情的条纹彩虹心特效视频演示如 过往项目 2018 年 5 月份项目汇总(共 30 个项目) 2018 年 4 月份项目汇总(共 8 个项目) 2018 年 6 月份发布的项目 ...
阅读 1185·2023-04-25 14:41
阅读 2718·2021-09-28 09:35
阅读 3818·2019-08-30 15:53
阅读 2120·2019-08-29 15:26
阅读 1263·2019-08-28 17:59
阅读 4529·2019-08-26 13:45
阅读 3000·2019-08-26 13:33
阅读 1804·2019-08-26 11:46