mrhwang

숫자 카운팅 애니메이션

· 3개월 전 · 411

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 팁자료실

+
제목 글쓴이 날짜 조회
2개월 전 조회 507
3개월 전 조회 511
3개월 전 조회 380
3개월 전 조회 519
3개월 전 조회 359
3개월 전 조회 478
3개월 전 조회 414
3개월 전 조회 565
3개월 전 조회 408
3개월 전 조회 440
3개월 전 조회 374
3개월 전 조회 623
3개월 전 조회 364
3개월 전 조회 384
3개월 전 조회 412
3개월 전 조회 274
3개월 전 조회 426
3개월 전 조회 424
3개월 전 조회 410
3개월 전 조회 374
3개월 전 조회 354
3개월 전 조회 295
3개월 전 조회 328
3개월 전 조회 293
3개월 전 조회 257
3개월 전 조회 444
3개월 전 조회 268
3개월 전 조회 341
4개월 전 조회 355
4개월 전 조회 487