围绕移动的div进行div轨道运动

时间:2014-08-24 14:01:06

标签: html css3 css-animations

我正在尝试在CSS3中学习animationkeyframes,我想知道如何围绕#moon-orbit进行#earth轨道运行?就像在现实生活中一样,你知道。

html, body {
    background-color : #000000;
    height : 90%;
    width : 90%;
}

#sun {
    background-color : #ba8f27;
    border-radius : 200px;
    box-shadow : 0 0 128px red;
    margin-top : -100px; 
    margin-left : -100px;
    left : 50%;
    height : 200px;
    position : absolute;
    top : 50%;
    width : 200px;
}

#earth-orbit {
    border: 2px solid #373737;
    border-radius: 50%;
    left: 50%;
    height: 500px;
    margin-top: -250px;
    margin-left: -250px;
    position: absolute;
    top: 50%;
    width: 500px;

    -webkit-animation: spin-right 10s linear infinite;
    -moz-animation: spin-right 10s linear infinite;
    -ms-animation: spin-right 10s linear infinite;
    -o-animation: spin-right 10s linear infinite;
    animation: spin-right 10s linear infinite;
}

#moon-orbit {
    border-radius: 50%;
    left: 50%;
    height: 500px;
    margin-top: -250px;
    margin-left: -250px;
    position: absolute;
    top: 50%;
    width: 500px;

    transform: rotate(360deg);
    transform-origin: 50% 100%;
}

#earth {
    background-color : #2d7ddc;
    border-radius : 50px;
    margin-left : -25px;
    margin-top : -25px;
    height : 50px;
    left : 50%;
    position : absolute;
    top : 0%;
    width : 50px;
}

#moon {
    background-color : #b2b2b2;
    border-radius : 50px;
    margin-left : -25px;
    margin-top : -25px;
    height : 16px;
    left : 43%;
    position : absolute;
    top : 0%;
    width : 16px;
}



@-webkit-keyframes spin-right {
    100% {
        -webkit-transform: rotate(360deg);
    }
}



<div id="sun"></div>

<div id="earth-orbit">
    <div id="earth"></div>

    <div id="moon-orbit">
        <div id="moon"></div>
    </div>
</div>

jsFiddle demo

1 个答案:

答案 0 :(得分:4)

您可以为月球添加一个支架并将其放置在地球的中心并旋转该div:

Demo

添加内容:

HTML:

<div class="moon-holder">
    <div id="moon"></div>
</div>

CSS:

.moon-holder {
    position: absolute;
    left: 50%;
    top: 0;
    width: 50px;
    height: 50px;
    margin: -25px 0 0 -25px;
    -webkit-animation: spin-right 5s linear infinite;
    -moz-animation: spin-right 5s linear infinite;
    -ms-animation: spin-right 5s linear infinite;
    -o-animation: spin-right 5s linear infinite;
    animation: spin-right 5s linear infinite;
}