본문 바로가기
프로그래밍

wecode 사전스터디(8/3) 로그

by Youngbin Kwon 2020. 8. 4.

개발한 것/배운 것


vanilla Javascript 30days 프로젝트 - 02.JS + CSS Clock

개요 : 현재 시간을 실시간으로 받아와서 시계를 표기

개발 필요 기능

1) html + css (시침, 분침, 초침의 기본 css 및 시간 변수에 따라 css 개발 필요)

2) 자바스크립트

 - 시간 함수 (Date)

 - 시간 함수에 따른 css 변경

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>JS + CSS Clock</title>
  </head>
  <body>
    <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>

    <style>
      html {
        background: #018ded url(https://unsplash.it/1500/1000?image=881&blur=5);
        background-size: cover;
        font-family: "helvetica neue";
        text-align: center;
        font-size: 10px;
      }

      body {
        margin: 0;
        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
        ); /* account for the height of the clock hands */
      }

      .hand {
        width: 50%;
        height: 6px;
        background: black;
        position: absolute;
        top: 50%;
        transform-origin: 100%; /* setting the clock hand to rotate at the right end*/
        transform: rotate(90deg);
        transition: all 0.05s;
        transition-timing-function: cubic-bezier(0.1, 2.7, 0.58, 1);
      }
    </style>

    <script src="app.js"></script>
  </body>
</html>
const secondHand = document.querySelector(".second-hand");
const minuteHand = document.querySelector(".min-hand");
const hourHand = document.querySelector(".hour-hand");

function setDate() {
  const now = new Date();

  const seconds = now.getSeconds();
  const secondsDegrees = (seconds / 60) * 360 + 90;
  secondHand.style.transform = `rotate(${secondsDegrees}deg)`;

  const minutes = now.getMinutes();
  const minuteDegrees = (minutes / 60) * 360 + 90;
  minuteHand.style.transform = `rotate(${minuteDegrees}deg)`;

  const hours = now.getHours();
  const hourDegrees = (hour / 12) * 360 + (mins / 60) * 30 + 90;
  hourHand.style.transform = `rotate(${hourDegrees}deg)`;
}

setInterval(setDate, 1000);

새로 배운 것

1. JS Date 함수

 - new Date로 선언

 - getHours, getMinutes, getSeconds : 현재 시간, 분, 초를 가져옴

2. setInterval(setDate, 1000) : setDate함수를 1000 milliseconds 마다 반복하여 수행

3. degree 설정 : 시, 분, 초에 맞는 각도 도출하여 css의 transform: rotate(각도)를 변경해줌


내일 할 것

1. freecodecamp 10문제 풀기

2. 30days 자바스크립트 3.강의 완료

3. udemy JS 강의 - movie 완료

4. github 공부 및 글 업로드

'프로그래밍' 카테고리의 다른 글

wecode 사전스터디(8/5) 로그  (0) 2020.08.06
wecode 사전스터디(8/4) 로그  (0) 2020.08.04
wecode 사전스터디(8/2) 로그  (0) 2020.08.02
wecode 사전스터디(7/31) 로그  (0) 2020.08.01
자바스크립트 객체(object)  (0) 2020.07.30

댓글