JavaScript 30 Day -- 02 時鐘

實現效果:

時鐘

關鍵點:

獲取時間 
    var data = new Date()
獲取小時/分鐘/秒
    date.getHours();
    date.getMinutes();
    date.getSeconds();
動畫:
    transform-origin: 100%; //旋轉的中心點
    transition: all 0.05s;  //進行動畫的時間
    transition-timing-function: cubic-bezier(0.1,2.7,0.58,1);   //進行運動的軌跡
    transform: rotate(90deg);   //旋轉的角度

css

*{
        padding: 0;
        margin: 0;
    }
    /*背景*/
    html {
        background: #018DED url(http://unsplash.it/1500/1000?image=881&blur=50) bottom center;
        background-size: cover;
        font-family: 'helvetica neue';
        text-align: center;
        font-size: 10px;
    }

    body {
        font-size: 2rem;
        display: flex;
        flex: 1;
        min-height: 100vh;
        align-items: center;
    }
    /*鬧鐘的框*/
    .clock {
        width: 30rem;
        height: 30rem;
        border: 20px solid white;
        border-radius: 50%;
        margin: 50px auto;
        position: relative;
        padding: 2rem;
        box-shadow: 0 0 0 4px rgba(0, 0, 0, 0.1),
                    inset 0 0 0 3px #EFEFEF,
                    inset 0 0 10px black,
                    0 0 10px rgba(0, 0, 0, 0.2);
    }
    /*調整指針的位置*/
    .clock-face {
        position: relative;
        width: 100%;
        height: 100%;
        transform: translateY(-3px);
        transform: rotate(90deg);
    }
    /*每個的指針的樣式*/
    .hand {
        width: 50%;
        position: absolute;
        top: 50%;
        transform-origin: right;
    }
    .hour-hand{
      width: 30%;
      height: 10px;
      left: 20%;
      background: white;
      border-radius: 10px;
    }
    .min-hand{
      width: 40%;
      height: 8px;
      left: 10%;
      background: black;
    }
    .second-hand{
        height: 2px;
        background: red;
    }

html

    <div class="clock">
        <div class="clock-face">
            <div class="hand hour-hand"></div>
            <div class="hand min-hand"></div>
            <div class="hand second-hand"></div>
        </div>
    </div>

javascript

function cl(){
            var date = new Date();

            var SH = document.querySelector(".second-hand");
            var SHD = (date.getSeconds()/60 )* 360;
            SH.style.transform = `rotate(${SHD}deg)`;


            var MH = document.querySelector(".min-hand");
            var MHD = (date.getMinutes()/60 )* 360;
            // console.log(SHD);
            MH.style.transform = `rotate(${MHD}deg)`;



            var HH = document.querySelector(".hour-hand");
            var HHD = (date.getHours()/12 )* 360;
            // console.log(SHD);
            HH.style.transform = `rotate(${HHD}deg)`;
        }
        setInterval(cl,1000);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章