๊ฐœ๋ฐœ๊ด€๋ จ/javascript&jquery

Counter.js ๋ฅผ ์จ์„œ ํ‹ฐ์Šคํ† ๋ฆฌ ๋ธ”๋กœ๊ทธ ๋ฐฉ๋ฌธ์ž ์ˆ˜ ์• ๋‹ˆ๋ฉ”์ด์…˜ ํšจ๊ณผ ๋„ฃ๊ธฐ

๐Ÿป๊ณฐ์ด๐Ÿป 2022. 8. 19.
728x90

์•ˆ๋…•ํ•˜์„ธ์š” ๊ณฐ์ด์—์š”:)

 

์˜ค๋Š˜์€ Counter.js๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ‹ฐ์Šคํ† ๋ฆฌ ๋ธ”๋กœ๊ทธ ๋ฐฉ๋ฌธ์ž์ˆ˜์— ํ™œ์šฉํ•˜์—ฌ ๋ฉ‹์ง€๊ฒŒ ๊พธ๋ฉฐ์ค„ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

 

counter.js๋Š” ์ •ํ•ด์ง„ ์ˆซ์ž๊นŒ์ง€ ์ฆ๊ฐ€ํ•˜๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜ ํšจ๊ณผ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š”๋ฐ์š”.

 

์˜ˆ์‹œ๋ฅผ ์‚ฌ์ง„์œผ๋กœ ๋ณด์—ฌ ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

์ด๋Ÿฐ ์‹์œผ๋กœ ๋ง์ด์—์š” ใ…Žใ…Ž

 

๋ฌด์Šจ ๋Š๋‚Œ์ธ์ง€ ๊ฐ์ด ์˜ค์‹œ์ฃ ??

 

ํ˜„์žฌ ์ €๋Š” ํ‹ฐ์Šคํ† ๋ฆฌ ๋ธ”๋กœ๊ทธ ๋ฐฉ๋ฌธ์ž ์ˆ˜์— ํ™œ์šฉ ์ค‘์ธ๋ฐ์š” ์ด๊ฑธ ํ•œ๋‹ค๊ณ  ํ•ด์„œ ๋ฐฉ๋ฌธ์ž์ˆ˜๊ฐ€ ์˜ค๋ฅด๊ณ  ๊ทธ๋Ÿฌ์ง„ ์•Š์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ƒฅ ์ž๊ธฐ๋งŒ์กฑ ๋ฐ ๋ณด๋Š” ์žฌ๋ฏธ ์ •๋„?

 

์ €๋Š” ์ž‘์—…์— ์•ž์„œ ์˜คํ”ˆ ์†Œ์Šค  GitHub๋ฅผ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

https://github.com/bfintal/Counter-Up

 

GitHub - bfintal/Counter-Up: Counter-Up is a lightweight jQuery plugin that counts up to a targeted number when the number becom

Counter-Up is a lightweight jQuery plugin that counts up to a targeted number when the number becomes visible. - GitHub - bfintal/Counter-Up: Counter-Up is a lightweight jQuery plugin that counts u...

github.com

 

๋ฌผ๋ก  ์—ฌ๊ธฐ์—๋„ ์ž˜ ์ •๋ฆฌ๋˜์–ด์žˆ์–ด์„œ ์ƒ๊ฐ๋ณด๋‹ค ์ ์šฉ์€ ์–ด๋ ต์ง€ ์•Š์•˜๋˜ ๊ฑฐ ๊ฐ™์•„์š”

 

์œ„ ์‚ฌ์ดํŠธ๋ฅผ ๋“ค์–ด๊ฐ€์‹œ๋ฉด

์ด๋ ‡๊ฒŒ ํŒŒ์ผ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค 

๋ฐ‘์— ๋‘ ๊ฐœ์˜ js๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

 

์ฒ˜์Œ์— cdn๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ js๋ฅผ include ํ•˜๋ ค๊ณ  ํ–ˆ์—ˆ๋Š”๋ฐ ์ž๊พธ ๋ถˆ๋Ÿฌ์˜ค์งˆ ๋ชปํ•œ๋‹ค๊ณ .... ์Šคํฌ๋ฆฝํŠธ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋”๋ผ๊ตฌ์š”..

 

<script src="http://cdnjs.cloudflare.com/ajax/libs/waypoints/2.0.3/waypoints.min.js"></script>
<script src="jquery.counterup.min.js"></script>

์œ„ ํŒŒ์ผ์— ์žˆ๋Š” ์ •๋ณด๋กœ๋Š” ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค๊ณ  ๋˜์–ด์žˆ์ง€๋งŒ https๊ฐ€ ์•„๋‹ˆ์–ด์„œ ๊ทธ๋Ÿฐ๊ฐ€? ํ•˜๊ณ  https๋กœ ๋ณ€๊ฒฝ์„ ํ•ด๋„ ์•ˆ๋˜๋”๋ผ๊ณ ์š”

 

๊ทธ๋ž˜์„œ ์ €๋Š” ๊ทธ๋ƒฅ ํ‹ฐ์Šคํ† ๋ฆฌ์— ์—…๋กœ๋“œํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

์šฐ์„  GitHub์—์„œ ์œ„์˜ ํŒŒ์ผ๋“ค์„ ๋‹ค ๋‹ค์šด๋ฐ›์•„์ฃผ์‹œ๊ณ  ์••์ถ•ํŒŒ์ผ์„ ํ’€์–ด์ฃผ์„ธ์š”.

 

๊ท€์ฐฎ์œผ์‹  ๋ถ„๋“ค์„ ์œ„ํ•ด ํŒŒ์ผ ์ฒจ๋ถ€ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

jquery.counterup.js
0.00MB
jquery.counterup.min.js
0.00MB

 

๊ทธ๋‹ค์Œ ํ‹ฐ์Šคํ† ๋ฆฌ ๊ด€๋ฆฌ ํƒญ > ๊พธ๋ฏธ๊ธฐ > ์Šคํ‚จ ํŽธ์ง‘ ํƒญ์„ ๋“ค์–ด๊ฐ€ ์ฃผ์„ธ์š”.

 html ํŽธ์ง‘์„ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”.

ํŒŒ์ผ ์—…๋กœ๋“œ๋ฅผ ๋ˆ„๋ฅด์‹œ๊ณ  +์ถ”๊ฐ€ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์œ„์— ๋งํ•œ JS 2๊ฐœ๋ฅผ ์—…๋กœ๋“œํ•ด์ฃผ์„ธ์š”.

 

์ €๋Š” ์ด๋ฏธ ๋˜์–ด์žˆ์–ด์„œ ํ‘œ๊ธฐ๋งŒ ํ•ด๋†จ์Šต๋‹ˆ๋‹ค.

 

ํฌ์ŠคํŒ… ์“ฐ๋ฉด์„œ ์•ˆ ์‚ฌ์‹ค์ธ๋ฐ counterup.js๋Š” ์—†์–ด๋„ ๋˜๋Š” ๊ฑฐ ๊ฐ™์•„์š”.

์Šคํ‚จ ํŽธ์ง‘ HTML์—์„œ <head> ํƒœ๊ทธ ์•ˆ์— ์—…๋กœ๋“œํ•ด๋†“์€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์™€์ค๋‹ˆ๋‹ค.

<script src="./images/jquery.counterup.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/waypoints/2.0.3/waypoints.min.js"></script>

์•„ ๊ทธ๋ฆฌ๊ณ  waypoints.min.js๋„ cdn์œผ๋กœ ๋ถˆ๋Ÿฌ์™€์ฃผ์–ด์•ผ ํ•ด์š” ์•ˆ ํ•˜๋‹ˆ๊นŒ ํšจ๊ณผ๊ฐ€ ์•ˆ ๋‚˜์˜ค๋Š” ๊ฑฐ ๊ฐ™์•„์š”

 

jquery๊ฐ€ ์—†์œผ์‹  ๋ถ„๋“ค์€ jquery๋„ ๋„ฃ์–ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค.

<script src="//t1.daumcdn.net/tistory_admin/lib/jquery/jquery-1.12.4.min.js"></script>

์œ„์— ํ‹ฐ์Šคํ† ๋ฆฌ์—์„œ ์ œ๊ณตํ•˜๋Š” cdn ์ œ์ด์ฟผ๋ฆฌ์ž…๋‹ˆ๋‹ค.

๋ฐฉ๋ฌธ์ž ์ˆ˜์— ์ ์šฉ์„ ํ•˜์‹œ๋ ค๋ฉด

 

ctrl + F๋กœ _count_total_ ์„ ์ฐพ์•„์ฃผ์‹  ๋’ค

 

๊ผญ ์ˆซ์ž ๋ถ€๋ถ„ ํƒœ๊ทธ์— ํด๋ž˜์Šค๋ฅผ ๋„ฃ์œผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์•ˆ ๊ทธ๋Ÿฌ๋ฉด NAN : NOT A NUMBER ๋œน๋‹ˆ๋‹ค.

 

์šฐ๋ฆฌ๊ฐ€ ์ฃผ์–ด์•ผ ํ•˜๋Š” ํด๋ž˜์Šค๋Š” counter์ž…๋‹ˆ๋‹ค.

 

๋ถํด๋Ÿฝ ์Šคํ‚จ ๊ธฐ์ค€์œผ๋กœ ์„ค๋ช…๋“œ๋ฆฌ์ž๋ฉด

 

<s_sidebar_element>
	<!-- ์ „์ฒด ๋ฐฉ๋ฌธ์ž -->
	<div class="count">
		<h2>์ „์ฒด ๋ฐฉ๋ฌธ์ž</h2>
		<p class="total"></p>
		<p>Today : </p>
		<p>Yesterday : </p>
	</div>
</s_sidebar_element>

์œ„๋Š” ๊ธฐ์กด ์†Œ์Šค์ž…๋‹ˆ๋‹ค.

 

<s_sidebar_element>
	<!-- ์ „์ฒด ๋ฐฉ๋ฌธ์ž -->
	<div class="count">
		<h2>์ „์ฒด ๋ฐฉ๋ฌธ์ž</h2>
		<p class="total"><span class="counter"></span></p>
		<p>Today : <span class="counter"></span></p>
		<p>Yesterday : <span class="counter"></span></p>
	</div>
</s_sidebar_element>

counter ํด๋ž˜์Šค๋ฅผ ์ค€ ๋ชจ์Šต. spanํƒœ๊ทธ ์ถ”๊ฐ€ ํ›„ ์ˆซ์ž ๋ถ€๋ถ„์—๋งŒ class๋ฅผ ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

์ ์šฉ๋œ ๋ชจ์Šต

 

<script>
	$('.counter').counterUp();
</scipt>

Jquery์šฉ ์…€๋ ‰ํ„ฐ ํ™œ์šฉํ•˜์—ฌ ์นด์šดํ„ฐ ํ•จ์ˆ˜ ํƒœ์šฐ๊ธฐ

<script>
	$('.counter').counterUp({
        delay: 10,
        time: 1000
    });
</scipt>

์˜ต์…˜๋„ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

delay : ์ˆซ์ž๋‹น ์ง€์—ฐ ์‹œ๊ฐ„(๋ฐ€๋ฆฌ์ดˆ) ์นด์šดํŠธ ์—…

time : ์นด์šดํŠธ ์—… ์• ๋‹ˆ๋ฉ”์ด์…˜์˜ ์ด ์ง€์† ์‹œ๊ฐ„(๋ฐ€๋ฆฌ์ดˆ)

1000๋‹น 1์ดˆ์ž…๋‹ˆ๋‹ค.

<!-- ๋ฐฉ๋ฌธ์ž ์ˆ˜ ์นด์šดํŠธ ์‹œ์ž‘ -->
<script>
    jQuery(document).ready(function($) {
        $('.counter').counterUp({
                delay: 20,
                time: 2000
        });
    });
</script>
<!-- ๋ฐฉ๋ฌธ์ž ์ˆ˜ ์นด์šดํŠธ ๋ -->

 

์œ„์˜ ๋ถ€๋ถ„์€ ๋ฐฉ๋ฌธ์ž์ˆ˜ class์— ํŽ˜์ด์ง€๊ฐ€ load ๋˜์—ˆ์„ ๋•Œ ์ž๋™์œผ๋กœ ๋˜๊ฒŒ๋” ํ•ด๋†“์€ ์†Œ์Šค์ž…๋‹ˆ๋‹ค.

 

๊ถ๊ธˆํ•˜์‹œ๊ฑฐ๋‚˜ ์ดํ•ด ์•ˆ ๋˜๋Š” ๋ถ€๋ถ„ ์žˆ์œผ๋ฉด ๋Œ“๊ธ€ ๋‹ฌ์•„์ฃผ์‹œ๋ฉด ์ตœ๋Œ€ํ•œ ๋„์™€๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋„์›€์ด ๋˜์…จ๋‹ค๋ฉด ํ•˜ํŠธ ๊พน~! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€