- JAVASCRIPT
- 숫자 카운팅 애니매이션 구현
- 댓글 : 0건
- 글쓴이 : mrhmh
- 작성일 : 2025-10-14 09:58
- 조회수 : 152
예제
<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>
스크립트
<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>
스크립트 넣은후 적용하고싶은곳에 data-count="숫자"
넣으면 동작
댓글목록
등록된 댓글이 없습니다.