2026, 새로운 도약을 시작합니다.

숫자 카운팅 애니메이션

· 2개월 전 · 273

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년 전 조회 4,594
2711 2개월 전 조회 323
2710 2개월 전 조회 226
2709 2개월 전 조회 393
2708 2개월 전 조회 232
2707 2개월 전 조회 275
2706 2개월 전 조회 274
2705 2개월 전 조회 144
2704 2개월 전 조회 295
2703 2개월 전 조회 268
2702 2개월 전 조회 282
2701 2개월 전 조회 279
2700 2개월 전 조회 225
2699 2개월 전 조회 173
2698 2개월 전 조회 221
2697 2개월 전 조회 192
2696 2개월 전 조회 165
2695 3개월 전 조회 321
2694 3개월 전 조회 161
2693 3개월 전 조회 215
2692 3개월 전 조회 219
2691 3개월 전 조회 364
2690 3개월 전 조회 269
2689 3개월 전 조회 290
2688 3개월 전 조회 239
2687 3개월 전 조회 451
2686 3개월 전 조회 503
2685 3개월 전 조회 635
2684 3개월 전 조회 371
2683 3개월 전 조회 513
2682 3개월 전 조회 325
🐛 버그신고