mrhwang

숫자 카운팅 애니메이션

· 3개월 전 · 402

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개월 전 조회 492
3개월 전 조회 503
3개월 전 조회 369
3개월 전 조회 512
3개월 전 조회 351
3개월 전 조회 469
3개월 전 조회 406
3개월 전 조회 553
3개월 전 조회 401
3개월 전 조회 433
3개월 전 조회 361
3개월 전 조회 610
3개월 전 조회 357
3개월 전 조회 380
3개월 전 조회 403
3개월 전 조회 266
3개월 전 조회 420
3개월 전 조회 415
3개월 전 조회 404
3개월 전 조회 369
3개월 전 조회 345
3개월 전 조회 286
3개월 전 조회 321
3개월 전 조회 288
3개월 전 조회 252
3개월 전 조회 437
3개월 전 조회 261
3개월 전 조회 339
3개월 전 조회 347
4개월 전 조회 480