ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฌธ์ œ

window.addEventListener('scroll', () => {
  box.style.width = `${window.pageYOffset}px`;
});

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์Šคํฌ๋กค๋œ Window.pageYoffset๊ฐ’ ๋งŒํผ ์ž„์˜์˜ ๋ฐ•์Šค ์š”์†Œ์˜ ๊ฐ€๋กœ ๋„ˆ๋น„๋ฅผ ๋™์ ์œผ๋กœ ๋Š˜์–ด๋‚˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค.

 

๋žœ๋”๋ง ๊ฒฐ๊ณผ

 

ํ™”๋ฉด์— ๋žœ๋”๋ง๋œ ์ž„์˜์˜ ๋ฐ•์Šค์˜ ๊ฐ€๋กœ ๋„ˆ๋น„๊ฐ€ ๋Š˜์–ด๋‚˜๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜ ํšจ๊ณผ๋Š” ์–ธ๋œป ๋ณด๋ฉด ์ž์—ฐ์Šค๋Ÿฌ์šด ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ ์Šคํฌ๋กค์„ ๋ฐฉํ–ฅํ‚ค๋ฅผ ํ†ตํ•ด ๋‚ด๋ ค๋ณธ๋‹ค๋ฉด ๋‹ค์†Œ ๋ถ€์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋Š๊ธฐ๋Š” ๊ฑฐ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋Ÿฌํ•œ ํ˜„์ƒ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ requestAnimationFrame์˜ ๋™์ž‘ ์›๋ฆฌ๋ฅผ ์‚ดํŽด๋ณด๊ณ  ๊ตฌํ˜„ํ•ด ๋ณด๊ณ ์ž ํ•œ๋‹ค.

 

requestAnimationFrame

https://developer.mozilla.org/ko/docs/Web/API/Window/requestAnimationFrame

 

window.requestAnimationFrame() - Web API | MDN

ํ™”๋ฉด์— ์ƒˆ๋กœ์šด ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์—…๋ฐ์ดํŠธํ•  ์ค€๋น„๊ฐ€ ๋ ๋•Œ๋งˆ๋‹ค ์ด ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋‹ค์Œ ๋ฆฌํŽ˜์ธํŠธ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ์ „์— ํ˜ธ์ถœ๋œ ์• ๋‹ˆ๋ฉ”์ด์…˜ ํ•จ์ˆ˜๋ฅผ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค. ์ฝœ๋ฐฑ์˜

developer.mozilla.org

The window.requestAnimationFrame() method tells the browser that you wish to perform an animation and requests that the browser calls a specified function to update an animation before the next repaint. The method takes a callback as an argument to be invoked before the repaint.

 

MDN์„ ๋ณด๋ฉด requestAnimationFrame์€ ๋‹ค์Œ ๋ฆฌํŽ˜์ธํŠธ ์ด์ „์— ํ•ด๋‹น ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ฆ‰, ๋ฃจํ”„๋ฅผ ๊ณ„์† ๋Œ๋ฉด์„œ ํ•ด๋‹น ์• ๋‹ˆ๋ฉ”์ด์…˜๊ณผ ๊ด€๋ จ๋œ ์—ฐ์‚ฐ ๊ณผ์ •์˜ ํ•จ์ˆ˜๋ฅผ ์ผ์ • ์กฐ๊ฑด์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต ํ˜ธ์ถœํ•œ๋‹ค๋Š” ๊ฒƒ์ธ๋ฐ, ์ด๋Ÿฌํ•œ ๋™์ž‘ ๋ฐฉ์‹์„ ์œ„ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์ง์ ‘ ๊ตฌํ˜„ํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค.

 

๋ถ€๋“œ๋Ÿฌ์šด ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์œ„ํ•ด์„œ๋Š” ์ถœ๋ฐœ ์ง€์ ์—์„œ๋ถ€ํ„ฐ ๋„์ฐฉ ์ง€์ ์—์„œ๊นŒ์ง€์˜ ์†๋„๊ฐ€ ์ผ์ •ํ•œ ๊ฒƒ๋ณด๋‹ค๋Š” ์ ์ง„์ ์œผ๋กœ ๊ฐ์†๋˜๋Š” ๋””์กธ๋ธŒ(desolve) ํšจ๊ณผ๊ฐ€ ํšจ๊ณผ์ ์ด๋‹ค.

 

๋‹ค์Œ ๊ทธ๋ฆผ์„ ํ†ตํ•ด ์ด๋ฅผ ํ™•์ธํ•ด ๋ณด์ž.

 

๋™์ž‘ ์›๋ฆฌ

 

๋ณ€ํ™”๋˜๋Š” ๊ฑฐ๋ฆฌ๊ฐ’์— ์˜๋„์ ์œผ๋กœ ๋ณ€ํ™”์˜ ํญ์„ ์ค„์ด๊ธฐ ์œ„ํ•œ ๊ฐ’์„ ์—ฐ์‚ฐํ•จ์œผ๋กœ์จ ์• ๋‹ˆ๋ฉ”์ด์…˜์— ๋”œ๋ ˆ์ด๋ฅผ ๊ฑธ์–ด์ฃผ์—ˆ๋‹ค.

 

ํ•ด๋‹น ์‚ฌํ•ญ์„ ์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

let delayedYOffset = delayedYOffset + (pageYOffset - delayedYOffset) * acc;

๋‹ค์Œ์œผ๋ก  ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๊ธฐ ์ „์— ๋ฌดํ•œ ๋ฃจํ”„๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜ ๋‚ด์—์„œ ํ•ด๋‹น ์‹์„ ํฌํ•จ์‹œ์ผœ ์ฃผ๋ฉด ๋ฐ˜๋ณต์ด ํ•œ๋ฒˆ์”ฉ ์ˆ˜ํ–‰๋  ๋•Œ๋งˆ๋‹ค ์ด๋™ํ•ด์•ผ ๋  ์ „์ฒด ๊ฑฐ๋ฆฌ๋Š” ์ค„์–ด๋“ค๊ฒŒ ๋˜๊ณ  ์ด์— ๋”ฐ๋ผ ์œ ๋™์ ์œผ๋กœ ๊ฐ€๋กœ ๋„ˆ๋น„๊ฐ€ ๋ณ€ํ™”๋˜๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋žœ๋”๋ง ๊ฒฐ๊ณผ

ํ˜„์žฌ๋Š” ๊ฐ„๋‹จํ•œ ๋ฐ•์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ํ…Œ์ŠคํŠธํ•ด ๋ณธ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์œก์•ˆ ์ƒ์œผ๋กœ ํฐ ์ฐจ์ด๋Š” ์—†์ง€๋งŒ ์—ฐ์†์ ์ธ ์ด๋ฏธ์ง€์˜ ๋‚˜์—ด์„ ์ด์šฉํ•˜์—ฌ ๋น„๋””์˜ค ํšจ๊ณผ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค๋Š” ๋“ฑ๊ณผ ๊ฐ™์ด ๋ฏธ๋””์–ด๋ฅผ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ‘œํ˜„ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์— ํšจ๊ณผ์ ์ด๋‹ค.

 

์ „์ฒด ์ฝ”๋“œ

const box = document.querySelector('.box');
		let acc = 0.1; //delay๋ฅผ ์œ„ํ•œ ๊ฐ’
		let delayedYOffset = 0;
		let rafId; //requestFrameAnimation๋ฅผ ํŠน์ • ์กฐ๊ฑด์ด ๋˜๋ฉด ๋ฉˆ์ถ”๊ธฐ ์œ„ํ•œ ์‹๋ณ„ ๋ณ€์ˆ˜_setTimeout๊ณผ ๋น„์Šทํ•œ ์›๋ฆฌ
		let rafState;

		window.addEventListener('scroll', () => {
			if (!rafState) {
				rafId = requestAnimationFrame(loop);
				rafState = true;
			}
		});

		function loop() {
			delayedYOffset = delayedYOffset + (pageYOffset - delayedYOffset) * acc;
			box.style.width = `${delayedYOffset}px`;

			rafId = requestAnimationFrame(loop); //๋ฆฌํŽ˜์ธํŠธ ๋˜๊ธฐ ์ „์— ๊ณ„์† loopํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ

			if (Math.abs(pageYOffset - delayedYOffset) < 1) { //์Šคํฌ๋กค ๋น„์œจ์— ๋”ฐ๋ฅธ end๊ฐ’๊นŒ์ง€ ๋„๋‹ฌํ–ˆ์„ ๊ฒฝ์šฐ ์ข…๋ฃŒ
				cancelAnimationFrame(rafId);
				rafState = false;
			}
		};

		loop();

 

๊ด€๋ จ ํฌ์ŠคํŒ…

https://choi95.tistory.com/5

 

Critical Rendering Path_์„ฑ๋Šฅ ๋ณด์žฅ ๋ Œ๋”๋ง

 ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋งˆ์šฐ์Šค ์ปค์„œ์— ๋”ฐ๋ผ ์›€์ง์ด๋Š” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•˜์˜€๋‹ค.  top๊ณผ left ์†์„ฑ์ด ์•„๋‹Œ translate ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ ์ด๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋Š”๋ฐ, ์ด๋Š” ๋žœ๋”๋ง ์„ฑ๋Šฅ์—์„œ ์ฐจ์ด๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด

choi95.tistory.com

https://choi95.tistory.com/9?category=857834 

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋Ÿฐํƒ€์ž„ ํ™˜๊ฒฝ(setTimeout, Promise then, RequestAnimationFrame)

 ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ™์€ ์žฌ๊ท€ ํ˜ธ์ถœ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋‘ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์œ„ ๋‘ ์ฝ”๋“œ์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๋‹ค๋ฅด๋‹ค.  setTimeout()์œผ๋กœ ์žฌ๊ท€ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ css์— ์ ์šฉ ํ•œ button:hover์˜ ์ƒ‰์ƒ๊ฐ’์ด ๋งˆ์šฐ์Šค ์ปค์„œ๋ฅผ ๋—

choi95.tistory.com

 

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ