테스트 사이트 - 개발 중인 베타 버전입니다

형님들 자바스크립트 질문드립니다. 채택완료

토악 1년 전 조회 3,551

http://sir.kr/data/editor/2403/2949574522_1709698070.7135.jpg" width="100%" />

 

위에같이 카운터가 올라가는 코드인데 첫번째 (누적매출 뒤에 115억) , 네번째 (창업만족도 뒤에 %) 두개만 따로 붙이려고 하는데 어디서 어떤 코드를 수정해야 될까요? ㅠㅠ 꼭좀 부탁드립니다..

 

 

<style> .counter { font-size: 30px; font-weight: 800; } .plus { color: red; font-size: 30px; font-weight: 800; } </style> <script> function animateCounter(id, endValue) { let startValue = 0; const duration = 2000; const stepTime = duration / 100; const counter = document.getElementById(id); const valueIncrement = endValue / 100; const interval = setInterval(() => { startValue += valueIncrement; counter.innerHTML = Math.round(startValue).toLocaleString() + '<span class="plus">+</span>'; if (startValue >= endValue) { clearInterval(interval); counter.innerHTML = endValue.toLocaleString() + '<span class="plus">+</span>'; } }, stepTime); } animateCounter("counter1", 56320);

댓글을 작성하려면 로그인이 필요합니다.

답변 2개

채택된 답변
+20 포인트
1년 전

</p>

<p><style>

    .counter { font-size: 30px; font-weight: 800; } .plus { color: red; font-size: 30px; font-weight: 800; }

</style></p>

<p><div>

    <div class="counter on" data-endvalue="56320" data-startvalue="115">115</div>

    <div class="counter">60<span class="plus">+</span></div>

    <div class="counter">1558<span class="plus">+</span></div>

    <div class="counter on" data-endvalue="95" data-startvalue="10">90</div>

</div></p>

<p><script>

function animateCounter(sel) {

    const counters = document.querySelectorAll(sel);

    

    counters.forEach((el) => {

        let startValue = el.dataset.startvalue ? Number(el.dataset.startvalue) : 0;

        const duration = 2000;

        const stepTime = duration / 100;

        const counter = el;

        const endValue = el.dataset.endvalue ? Number(el.dataset.endvalue) : 0;

        const valueIncrement = endValue / 100;

        const interval = setInterval(() => {

            startValue += valueIncrement;

            counter.innerHTML = Math.round(startValue).toLocaleString() + '<span class="plus">+</span>';

            if (startValue >= endValue) {

                clearInterval(interval);

                counter.innerHTML = endValue.toLocaleString() + '<span class="plus">+</span>';

            }

        }, stepTime);

    });

}

animateCounter(".counter.on");

</script></p>

<p>

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

e
1년 전

'<span class="plus">+</span>' 

이 부분을 id 에 따라서 다르게 하면 될 듯 하네요.

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

토악
1년 전
자세히 알려주시면 안될까요? 완전 초짜라..ㅠㅠ

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인