mrhwang

숫자 카운팅 애니메이션

· 4개월 전 · 607

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개월 전 조회 692
3개월 전 조회 690
3개월 전 조회 559
3개월 전 조회 713
4개월 전 조회 535
4개월 전 조회 689
4개월 전 조회 611
4개월 전 조회 801
4개월 전 조회 617
4개월 전 조회 650
4개월 전 조회 582
4개월 전 조회 854
4개월 전 조회 570
4개월 전 조회 572
4개월 전 조회 608
4개월 전 조회 479
4개월 전 조회 619
4개월 전 조회 608
4개월 전 조회 601
4개월 전 조회 566
4개월 전 조회 572
4개월 전 조회 466
4개월 전 조회 515
4개월 전 조회 466
4개월 전 조회 437
4개월 전 조회 614
4개월 전 조회 442
4개월 전 조회 509
4개월 전 조회 526
5개월 전 조회 648