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

숫자 카운팅 애니메이션

· 2개월 전 · 272

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,592
2741 어제 조회 74
2740 3일 전 조회 81
2739 1주 전 조회 194
2738 1주 전 조회 199
2737 1주 전 조회 168
2736 1주 전 조회 267
2735 2주 전 조회 271
2734 3주 전 조회 254
2733 1개월 전 조회 257
2732 1개월 전 조회 292
2731 1개월 전 조회 260
2730 1개월 전 조회 217
2729 1개월 전 조회 343
2728 1개월 전 조회 237
2727 1개월 전 조회 412
2726 1개월 전 조회 247
2725 1개월 전 조회 322
2724 1개월 전 조회 351
2723 1개월 전 조회 259
2722 1개월 전 조회 291
2721 1개월 전 조회 205
2720 2개월 전 조회 298
2719 2개월 전 조회 301
2718 2개월 전 조회 195
2717 2개월 전 조회 328
2716 2개월 전 조회 197
2715 2개월 전 조회 305
2714 2개월 전 조회 265
2713 2개월 전 조회 367
2712 2개월 전 조회 283
🐛 버그신고