mrhwang

숫자 카운팅 애니메이션

· 4개월 전 · 592

HTML구조 (예제)

 

<span>

  TODAY : <span id="today-count" data-count="<?php echo $visit[1] ?>">0</span> /

  TOTAL : <h4><span id="total-count" data-count="<?php echo $visit[4] ?>">0</span></h4>

</span>

 

 

적용하고싶은곳에

data-count를 추가하여 넣으면 동작합니다.

0은 처음 실행시 기본값숫자입니다.

 

스크립트

 

<script>

  function animateCounter(id, target) {

    const el = document.getElementById(id);

    let count = 0;

    const speed = Math.ceil(target / 100); // 시간 조절

 

    const interval = setInterval(() => {

      count += speed;

      if (count >= target) {

        count = target;

        clearInterval(interval);

      }

      el.textContent = count;

    }, 20); // 속도 조절 가능

  }

 

  window.onload = () => {

    const todayTarget = parseInt(document.getElementById('today-count').dataset.count);

    const totalTarget = parseInt(document.getElementById('total-count').dataset.count);

 

    animateCounter('today-count', todayTarget);

    animateCounter('total-count', totalTarget);

  };

</script>

 

예제는 제 홈페이지 링크 참조바랍니다~

|
댓글을 작성하시려면 로그인이 필요합니다.

그누보드5 팁자료실

+
제목 글쓴이 날짜 조회
3개월 전 조회 671
3개월 전 조회 673
3개월 전 조회 543
3개월 전 조회 698
3개월 전 조회 522
3개월 전 조회 668
4개월 전 조회 594
4개월 전 조회 785
4개월 전 조회 598
4개월 전 조회 634
4개월 전 조회 571
4개월 전 조회 834
4개월 전 조회 541
4개월 전 조회 554
4개월 전 조회 593
4개월 전 조회 448
4개월 전 조회 604
4개월 전 조회 587
4개월 전 조회 584
4개월 전 조회 548
4개월 전 조회 557
4개월 전 조회 443
4개월 전 조회 499
4개월 전 조회 449
4개월 전 조회 413
4개월 전 조회 594
4개월 전 조회 425
4개월 전 조회 489
4개월 전 조회 500
4개월 전 조회 628