mrhwang

숫자 카운팅 애니메이션

· 4개월 전 · 609

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개월 전 조회 696
3개월 전 조회 702
3개월 전 조회 566
3개월 전 조회 718
4개월 전 조회 543
4개월 전 조회 695
4개월 전 조회 614
4개월 전 조회 807
4개월 전 조회 624
4개월 전 조회 659
4개월 전 조회 593
4개월 전 조회 861
4개월 전 조회 575
4개월 전 조회 575
4개월 전 조회 610
4개월 전 조회 484
4개월 전 조회 624
4개월 전 조회 611
4개월 전 조회 604
4개월 전 조회 573
4개월 전 조회 578
4개월 전 조회 472
4개월 전 조회 521
4개월 전 조회 469
4개월 전 조회 440
4개월 전 조회 619
4개월 전 조회 449
4개월 전 조회 515
4개월 전 조회 531
5개월 전 조회 651