Tailwind.css의 간단한 애니메이션 아날로그 시계

Tailwind.css에서만 애니메이션으로 아주 간단한 아날로그 시계를 만드는 방법

시계태엽 순풍

내가 작업한 프로젝트의 세부 정보 페이지를 디자인하는 동안(내 포트폴리오 페이지에서 사용 가능) 프로젝트 기간 옆에 렌더링되는 간단한 애니메이션 아날로그 시계도 구현했습니다.

새로운 외부 라이브러리나 비디오를 사용하고 싶지 않았기 때문에 Tailwind.css와 훌륭한 애니메이션 유틸리티를 통해 시계를 구현하기로 결정했습니다.

코드 예제

그냥 재사용을 위한 몇 가지 코드 예제를 제공할 때 가장 유용할 것 같아서 다른 할 말은 없습니다.

모든 예제의 레이아웃은 매우 유사하거나 거의 동일합니다. 나는 서로 위에 절대 위치를 지정하여 일부 div 상자를 렌더링합니다. 각 상자에는 사용 가능한 가장 작은 너비만 있으므로 시계에서 볼 수 있는 커서로 나타납니다.

마지막 예는 12시에 시작하는 방법을 보여줍니다. 처음 두 개의 예는 행에 배치된 대로 각각 3시에 시작합니다.

예 1: 가장 단순한 시계

Image dbead4b8e463

export function Clock() {
  return (
    <div className="flex justify-center py-10 group">
      <div className="relative flex items-center justify-end w-20 h-20 overflow-hidden bg-gray-900 rounded-full ">
        <div className="absolute w-1/2 h-1 bg-white rounded-full origin-left -rotate-12 group-hover:rotate-[215deg] duration-1000 ease-in-out" />

        <div className="absolute w-1/2 h-1  origin-left rotate-[70] group-hover:rotate-[340deg] duration-1000 ease-in-out">
          <div className="w-2/3 h-full bg-white rounded-full" />
        </div>

        <div className="absolute flex justify-center flex-1 w-full">
          <div className="w-1 h-1 bg-white rounded-full" />
        </div>
      </div>
    </div>
  );
}

예 2: 테두리와 표시기가 있는 시계

Image 06597167696f

import React from "react";
// Note that I'm using 'clsx' for
// composing classnames.
import clsx from "clsx";

export function Clock() {
  return (
    <div className="relative flex items-center justify-end w-20 h-20 overflow-hidden rounded-full ring-gray-600 ring-1">
      {Array.from({ length: 8 }, (_, i) => (
        <div
          key={i}
          style={{ height: 1 }}
          className={clsx("absolute w-1/2 origin-left flex justify-end", {
            "rotate-0": i === 0,
            "rotate-45": i === 1,
            "rotate-90": i === 2,
            "rotate-[135deg]": i === 3,
            "rotate-180": i === 4,
            "rotate-[225deg]": i === 5,
            "rotate-[270deg]": i === 6,
            "rotate-[315deg]": i === 7,
          })}>
          <div className="w-1/3 h-full bg-gray-600 rounded-full" />
        </div>
      ))}

      <div className="absolute w-1/2 h-1 bg-gray-300 rounded-full origin-left -rotate-12 group-hover:rotate-[215deg] duration-1000 ease-in-out" />

      <div className="absolute w-1/2 h-1  origin-left rotate-[85deg] group-hover:rotate-[340deg] duration-1000 ease-in-out">
        <div className="w-2/3 h-full bg-gray-300 rounded-full" />
      </div>

      <div className="absolute flex justify-center flex-1 w-full">
        <div className="w-1 h-1 bg-gray-300 rounded-full" />
      </div>
    </div>
  );
}

예 3: 자정에 시작하는 시계

Image c2d6bdde9e1e

export function Clock() {
  return (
    <div className="flex justify-center py-10 group">
      <div className="relative flex flex-col items-center justify-start w-20 h-20 overflow-hidden bg-gray-900 rounded-full ">
        <div className="absolute w-1 duration-1000 ease-in-out origin-bottom bg-gradient-to-t from-white to-red-400 rounded-full h-1/2 group-hover:rotate-[200deg]" />

        <div className="absolute h-1/2 w-1 origin-bottom rotate-[55deg] group-hover:rotate-[340deg] duration-1000 ease-in-out flex flex-col justify-end">
          <div className="w-full rounded-full bg-gradient-to-t from-white to-blue-400 h-2/3" />
        </div>

        <div className="absolute flex items-center justify-center flex-1 w-full h-full">
          <div className="w-1 h-1 bg-white rounded-full" />
        </div>
      </div>
    </div>
  );
}