본문 바로가기

Frontend/CSS

Countdown with CSS

반응형

Let’s see how to implement a countdown using CSS animation

Implementation

HTML

Add numbers to countdown as needed

<div class="contents">
  <div class="count">
    <div class="num">1</div>
    <div class="num">2</div>
    <div class="num">3</div>
    <div class="num">Go</div>
  </div>
</div>

CSS

Use CSS animation delay to show the counter in order.

/* count */
.count {
  height: 50px;
  width: 50px;
  position: relative;
  margin: auto;
  overflow: hidden;
}

.count .num {
  height: 40px;
  position: absolute;
  bottom: -5px;
  left: 50%;
  transform-origin: bottom;
  transform: translateX(-50%) rotate(180deg);
  font-size: 1.5rem;
}

.count .num:nth-child(1) {
  animation: spin 2s cubic-bezier(0.075, 0.82, 0.165, 1) forwards;
}

.count .num:nth-child(2) {
  animation: spin 2s 2s cubic-bezier(0.075, 0.82, 0.165, 1) forwards;
}

.count .num:nth-child(3) {
  animation: spin 2s 4s cubic-bezier(0.075, 0.82, 0.165, 1) forwards;
}

.count .num:nth-child(4) {
  animation: spin 2s 6s cubic-bezier(0.075, 0.82, 0.165, 1) forwards;
}

@keyframes spin {
  0% {
    transform: translateX(-50%) rotate(180deg);
  }

  30% {
    transform: translateX(-50%) rotate(-10deg);
  }

  60% {
    transform: translateX(-50%) rotate(10deg);
  }

  100% {
    transform: translateX(-50%) rotate(-180deg);
  }
}

We have seen a way to implement a countdown using CSS animation.

 

728x90
반응형

'Frontend > CSS' 카테고리의 다른 글

Toggle button with CSS  (5) 2023.03.27