[30일 챌린지 Day-2] 시계 만들기

2022. 7. 25. 12:14·개발/html, css, js
728x90
반응형

기본값과 결과를 html로 띄어놓고 코딩해보세요

 

1. 기본값

<!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%;
    }

  </style>

  <script>


  </script>
</body>
</html>

2. 결과

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>JS + CSS Clock</title>
    <script
      src="https://code.jquery.com/jquery-3.6.0.min.js"
      integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
      crossorigin="anonymous"
    ></script>
  </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%;
      }
      .second-hand {
        transition: linear 1s;
      }
    </style>

    <script>
      const hourHand = document.querySelector(".hour-hand");
      const minHand = document.querySelector(".min-hand");
      const secHand = document.querySelector(".second-hand");

      let Current = (function () {
        function Current() {
          this.hour = new Date().getHours() + 3;
          this.min = new Date().getMinutes() + 15;
          this.sec = new Date().getSeconds() + 15;
        }
        return Current;
      })();
      setInterval(() => {
        let clock = new Current();

        let hourdeg =
          clock.hour * 30 + (clock.min / 60) * 30 + (clock.sec / 3600) * 30;
        hourHand.style.transform = `rotate(${hourdeg}deg)`;
        hourHand.style.transformOrigin = `right`;

        let mindeg = clock.min * 6 + (clock.sec / 60) * 6;
        minHand.style.transform = `rotate(${mindeg}deg)`;
        minHand.style.transformOrigin = `right`;

        let secdeg = clock.sec * 6;
        secHand.style.transform = `rotate(${secdeg}deg)`;
        secHand.style.transformOrigin = `right`;
      }, 1000);
    </script>
  </body>
</html>

 

3. 리뷰

 

처음 시침,분침,초침이 9시방향을 가리키고 있어서 12시방향으로 옮겨주기 위해 3시간, 15분,15초 씩 더해줬다.

setInterval 로 1초마다 한번씩 값을 갱신하도록 작성했다.

초침은 60초에 360도를 움직이므로 1초에 6도 움직인다.

분침은 60분에 360도를 움직이므로 1분에 6도 움직인다.

여기에 60초에 1분이므로 60초에 6도움직이기 때문에 초/60 *6 을 추가한다.

시침은 12시간에 360도를 움직이므로 1시간 30도, 60분에 30도움직이고, 3600초에 30도 움직이므로 값을 추가한다.

 

728x90
반응형

'개발 > html, css, js' 카테고리의 다른 글

[30일 챌린지 Day-4] 배열,객체 알고리즘  (0) 2022.07.25
[30일 챌린지 Day-3] input값 적용하기  (0) 2022.07.25
[JavaScript] 농구게임 만들기  (0) 2022.06.21
[HTML, JavaScript] 정규표현식  (0) 2022.06.21
[JS, HTML, CSS] 이벤트  (0) 2022.06.17
'개발/html, css, js' 카테고리의 다른 글
  • [30일 챌린지 Day-4] 배열,객체 알고리즘
  • [30일 챌린지 Day-3] input값 적용하기
  • [JavaScript] 농구게임 만들기
  • [HTML, JavaScript] 정규표현식
TeTedo.
TeTedo.
  • TeTedo.
    TeTedo 개발 일기
    TeTedo.
  • 전체
    오늘
    어제
    • 분류 전체보기 (319)
      • 개발 (274)
        • Article (4)
        • 정리 (21)
        • Spring Boot (17)
        • JPA (2)
        • JAVA (6)
        • Database (4)
        • 자료구조 (11)
        • 알고리즘 (32)
        • React (20)
        • Docker (10)
        • node.js (18)
        • Devops (11)
        • Linux (4)
        • TypeScript (3)
        • Go (10)
        • HyperLedger (4)
        • BlockChain (43)
        • html, css, js (48)
        • CS (3)
        • AWS (3)
      • 모아두고 나중에 쓰기 (3)
      • 팀프로젝트 (18)
        • SNS(키보드워리어) (9)
        • close_sea (9)
      • 개인프로젝트 (1)
        • Around Flavor (1)
        • CHAM (13)
        • ethFruitShop (5)
      • 독서 (0)
        • 스프링부트와 AWS로 혼자 구현하는 웹 서비스 (0)
  • 블로그 메뉴

    • 홈
    • 개발일기
    • CS
    • 실습
    • 코딩테스트
    • 웹
    • Go
    • node.js
    • 팀플
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    html
    명령어
    30일챌린지
    프로그래머스
    nodejs
    블록체인
    React
    mysql
    CSS
    js
    erc20
    go언어
    하이퍼레저
    go
    컨테이너
    30일 챌린지
    node
    node.js
    ERC721
    도커
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
TeTedo.
[30일 챌린지 Day-2] 시계 만들기
상단으로

티스토리툴바