55.纯 CSS 创作一个太阳、地球、月亮的运转模型

原文地址:https://segmentfault.com/a/1190000015313341

感想:主要运用边框、伪元素、动画。

HTML code: 

<div class="container">
    <div class="sun"></div>
    <div class="earth">
        <div class="moon"></div>
    </div>
</div>

CSS code:

html, body {
    margin: 0;
    padding: 0;
}
/* 设置body子元素水平垂直居中 */
body{
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: black;
    /* 隐藏突出的部分 */
    overflow: hidden;
}
/* 设置.container容器尺寸 */
.container{
    position: relative;
    font-size: 10px;
    width: 40em;
    height: 40em;
    /* border: 1px solid white; */
}
/* 画出太阳 */
.sun{
    position: absolute;
    top: 15em;
    left: 15em;
    width: 10em;
    height: 10em;
    border-radius: 50%;
    box-shadow: 0 0 3em white;
    background-color: yellow;
}
/* 画出地球和月球的轨迹 */
.earth,
.earth .moon{
    position: absolute;
    border-width: 0.1em 0.1em 0 0;
    border-style: solid;
    border-color: white transparent transparent transparent;
    border-radius: 50%;
}
.earth{
    top: 5em;
    left: 5em;
    width: 30em;
    height: 30em;
    /* border: 1px solid white; */
    /* 设置动画  orbit: 轨道*/
    animation: orbit 36.5s linear infinite;
}
.earth .moon{
    top: 0;
    right: 0;
    width: 8em;
    height: 8em;
    /* border: 1px solid white; */
    animation: orbit 2.7s linear infinite;
}
@keyframes orbit {
    to {
        transform: rotate(360deg);
    }
}
/* 用伪元素画出地球和月球 */
.earth::before,
.earth .moon::before{
    position: absolute;
    content: '';
    border-radius: 50%;
}
.earth::before{
    top: 2.8em;
    right: 2.5em;
    width: 3em;
    height: 3em;
    background-color: aqua;
}
.earth .moon::before{
    top: 0.8em;
    right: 0.2em;
    width: 1.2em;
    height: 1.2em;
    background-color: silver;
}

 

posted on 2019-03-27 21:44  人生与戏 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/FlyingLiao/p/10611105.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值