ํฐ์คํ ๋ฆฌ ๋ทฐ
๋ค์๊ณผ ๊ฐ์ด Progress-Bar๋ฅผ ๊ตฌํํ ๋, ๋ฒํผ์ ํด๋ฆญํ ๋ค์ ์งํ ๋ฐ ์์์ width ๊ฐ์ด 25%๊ฐ ๋์์ ๊ฒฝ์ฐ ํด๋น ์ ๋๋ฉ์ด์ ์
์ข ๋ฃ์ํค๊ธฐ ์ํด์ ๋ค์๊ณผ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ฑํ์๋ค.
if(elem.style.getPropertyValue('width') === range * current) { //ํ์ฌ ์งํ ๋ฐ ๊ฐ๋ก ๋๋น๊ฐ ์ง์ 25%์ ๋๋ฌํ์ ๊ฒฝ์ฐ
clearInterval(intervalId); //์ ๋๋ฉ์ด์
์ข
๋ฃ
current++;
}
ํ์ง๋ง ์ํ๋ ๊ฒฐ๊ณผ๋ฅผ ์ป์ง ๋ชปํ๊ณ ์งํ ๋ฐ๋ ๋์์ด ์ข ๋ฃ๋์ง ์์ ์ฑ ๋ฌดํ ๋ฃจํ๋ก ๋น ์ง๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์๋ค.
getPropertyValue์ ๋ฐํ๊ฐ
๋ฌธ์ getPropertyValue๊ฐ ๋ฐํํ๋ ๋ฐ์ดํฐ ํ์ ์ ๋ฌธ์ ๊ฐ ์์๋ค.
The CSSStyleDeclaration.getPropertyValue() method interface returns a DOMString containing the value of a specified CSS property.
A DOMString is a sequence of 16-bit unsigned integers, typically interpreted as UTF-16 code units. This corresponds exactly to the JavaScript primitive String type
getPropertyValue๊ฐ ๋ฐํํ๋ ๊ฐ์ ๋ฐ์ดํฐ ํ์ ์ String type์ด๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ ํ์ ๋ถ์ผ์น๋ก ํด๋น ์กฐ๊ฑด๋ฌธ์ด ์ ๋๋ก ์ํํ์ง ์์๋ ๊ฒ์ด๋ค.
if(parseFloat(elem.style.getPropertyValue('width')) === range * current) {
clearInterval(intervalId);
current++;
}
์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๋ค์๊ณผ ๊ฐ์ด parseFloat()๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ ํ์ ์ ์ซ์๋ก ํ๋ณํ ํด์ฃผ์๋ค.
(ํด๋น ์งํ ๋ฐ 10ms ๋น 2.5์ฉ ์ฆ๊ฐํ๊ธฐ ๋๋ฌธ์ ์์์ ์ ๋ฐํํ๋ parseFloat๋ฅผ ์ฌ์ฉ)
'JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์คํฌ๋กค ์์น ๊ฐ์ ธ์ค๊ธฐ (0) | 2021.07.23 |
---|---|
ํด๋์ค๋ ์์ด๋ ๊ฐ์ด ์๋ Element ๊ฐ์ ธ์ฌ ๋ ๋ฌธ์ (0) | 2021.07.22 |
setTimeout์ผ๋ก setInterval ๊ตฌํ (0) | 2021.07.18 |
pipe operator๋ฅผ ํตํด ์คํผ๋ ์ดํฐ ์ฒ๋ฆฌ (0) | 2021.07.17 |
RxJS debounceTime operator๋ฅผ ํตํด Event request ์ค์ด๊ธฐ (0) | 2021.07.17 |