๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์‹œ๋„/๊ตญ๋น„์ง€์›

[D+52] Front-end ๋ฏธ๋‹ˆํ”„๋กœ์ ํŠธ 8์ผ์ฐจ : ์„น์…˜ ์™„์„ฑ

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2022. 12. 5.

 

๊ตญ๋น„์ง€์› D+52

Front-end ๋ฏธ๋‹ˆํ”„๋กœ์ ํŠธ 8์ผ์ฐจ

- ์„น์…˜ ์™„์„ฑ -

 


 

์˜ค๋Š˜ ํ•œ ์ผ

- section2 ์ถ”๊ฐ€ ๋ฐ ์ „๋ฐ˜์ ์ธ section ์™„์„ฑ

- section2 ๋™์˜์ƒ ์ถ”๊ฐ€

 

ํŒŒ์ผ ๊ตฌ์„ฑ ๋ฐ ๋‚ด์šฉ

 


• style.css

footer {
  /* flex - footer์•ˆ์— ์žˆ๋Š” ๋‚ด์šฉ์„ ๊ฐ€์šด๋ฐ๋กœ ์ •๋ ฌํ•ด์ค„ ๊ฒƒ์ด๋‹ค. */
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 5em;
  background-color: gray;
  color: black;
  font-weight: bold;
}

video {
  margin: 0 auto;
  border: 3px solid palegoldenrod;
}

 

• index.html

 <!-- section2 -->
<section class="section2">
    <p class="section2-message">๋ง›์žˆ๋Š” ์‚ฌ๊ณผ</p>
    <video src="./video/apple.mp4" loop muted autoplay width="1000px"></video>
</section>

 

• main.js

// sectionSet : n๋ฒˆ์งธ ์„น์…˜์— ๋Œ€ํ•œ ๊ฐ์ข… ์ •๋ณด์ง‘ํ•ฉ
	const sectionSet = [
		// section0
		{
			height: 0,
			multiplyValue: 5,
			elemInfo: {
				section: document.querySelector(".section0"),
				message: [
					document.querySelector("#section0-message-0"),
					document.querySelector("#section0-message-1"),
					document.querySelector("#section0-message-2"),
					document.querySelector("#section0-message-3"),
				],
				canvas: document.querySelector("#section0-canvas")
			},
			opacitySettingsValues: {	// ํˆฌ๋ช…๋„ ์• ๋‹ˆ๋ฉ”์ด์…˜ ์…‹ํŒ… ๊ฐ’๋“ค
				message0_opacity_out: [0, 1, { start: 0.05, end: 0.14 }],
				message0_opacity_in: [1, 0, { start: 0.15, end: 0.24 }],

				message1_opacity_out: [0, 1, { start: 0.25, end: 0.34 }],
				message1_opacity_in: [1, 0, { start: 0.35, end: 0.44 }],

				message2_opacity_out: [0, 1, { start: 0.45, end: 0.54 }],
				message2_opacity_in: [1, 0, { start: 0.55, end: 0.64 }],

				message3_opacity_out: [0, 1, { start: 0.65, end: 0.74 }],
				message3_opacity_in: [1, 0, { start: 0.75, end: 0.84 }],
			},
			tanslateYSettingsValues: {	// ๊ธ€์ž ์œ„์น˜ ์• ๋‹ˆ๋ฉ”์ด์…˜ ์…‹ํŒ… ๊ฐ’๋“ค
				message0_translateY_out: [0, -20, { start: 0.05, end: 0.14 }],
				message0_translateY_in: [-20, -40, { start: 0.15, end: 0.24 }],

				message1_translateY_out: [0, -20, { start: 0.25, end: 0.34 }],
				message1_translateY_in: [-20, -40, { start: 0.35, end: 0.44 }],

				message2_translateY_out: [0, -20, { start: 0.45, end: 0.54 }],
				message2_translateY_in: [-20, -40, { start: 0.55, end: 0.64 }],

				message3_translateY_out: [0, -20, { start: 0.65, end: 0.74 }],
				message3_translateY_in: [-20, -40, { start: 0.75, end: 0.84 }],
			},
			imageSettingsValues: {	// ์บ”๋ฒ„์Šค ์ด๋ฏธ์ง€ ์• ๋‹ˆ๋ฉ”์ด์…˜ ์…‹ํŒ… ๊ฐ’๋“ค
				imageCount: 500,
				imageRange: [0, 499, {start: 0, end: 1}],
				image_in: [1, 0, {start: 0.85, end: 1}],
				images: []
			}
		},
		// section1
		{
			height: 0,
			multiplyValue: 2,
			elemInfo: {
				section: document.querySelector(".section1")
			},
		},
		// section2
		{
			height:0,
			multiplyValue: 1,
			elemInfo: {
				section: document.querySelector(".section2"),
				message: document.querySelector(".section2-message")
			}
		}
	];
    
    /////////////////////////////////////////////////////////
	// ์ผ๋ฐ˜ ํ•จ์ˆ˜
    
    // playAnimation : ์„น์…˜์— ๋”ฐ๋ฅธ ์• ๋‹ˆ๋ฉ”์ด์…˜ ๋™์ž‘ ์‹œํ‚ค๊ธฐ
	//  - parameter : x
	//  - return : x
	const playAnimation = function () 
	{
		let value;
		let opacityValue;
		let translateYValue;
		let messageValue;

		let imageIndex = 0;
		

		const yOffsetRate = sectionYOffset / sectionSet[currentSection].height;

		let canvasCtx;
		let images;
		let imageRange;

		switch (currentSection) 
		{
			case 0:		// section0

				// cnavas info
				canvasCtx = sectionSet[currentSection].elemInfo.canvas.getContext('2d');
				images = sectionSet[currentSection].imageSettingsValues.images;
				imageRange = sectionSet[currentSection].imageSettingsValues.imageRange;

				imageIndex = Math.floor(calcValue(imageRange));
				canvasCtx.drawImage(images[imageIndex], 0, 0);

				// opacitiy ๊ฐ’ ์ดˆ๊ธฐํ™”
				sectionSet[0].elemInfo.message.map((el) => {
					el.style.opacity = 0;
				});

				opacityValue = sectionSet[0].opacitySettingsValues;
				translateYValue = sectionSet[0].tanslateYSettingsValues;
				messageValue = sectionSet[0].elemInfo.message;

				// message0
				if (yOffsetRate < 0.15) 
				{
					value = calcValue(opacityValue.message0_opacity_out);
					messageValue[0].style.opacity = value;
					
					value = calcValue(translateYValue.message0_translateY_out);
					messageValue[0].style.transform = `translateY(${value}%)`;
				} 
				else if (yOffsetRate >= 0.15 && yOffsetRate < 0.25) 
				{
					value = calcValue(opacityValue.message0_opacity_in);
					messageValue[0].style.opacity = value;

					value = calcValue(translateYValue.message0_translateY_in);
					messageValue[0].style.transform = `translateY(${value}%)`;
				}

				// message1
				else if (yOffsetRate >= 0.25 && yOffsetRate < 0.35) 
				{
					value = calcValue(opacityValue.message1_opacity_out);
					messageValue[1].style.opacity = value;

					value = calcValue(translateYValue.message1_translateY_out);
					messageValue[1].style.transform = `translateY(${value}%)`;
				} 
				else if (yOffsetRate >= 0.35 && yOffsetRate < 0.45) 
				{
					value = calcValue(opacityValue.message1_opacity_in);
					messageValue[1].style.opacity = value;

					value = calcValue(translateYValue.message1_translateY_in);
					messageValue[1].style.transform = `translateY(${value}%)`;
				}

				// message2
				else if (yOffsetRate >= 0.45 && yOffsetRate < 0.55) 
				{
					value = calcValue(opacityValue.message2_opacity_out);
					messageValue[2].style.opacity = value;

					value = calcValue(translateYValue.message2_translateY_out);
					messageValue[2].style.transform = `translateY(${value}%)`;
				} 
				else if (yOffsetRate >= 0.55 && yOffsetRate < 0.65) 
				{
					value = calcValue(opacityValue.message2_opacity_in);
					messageValue[2].style.opacity = value;

					value = calcValue(translateYValue.message2_translateY_in);
					messageValue[2].style.transform = `translateY(${value}%)`;
				}

				// message3
				else if (yOffsetRate >= 0.65 && yOffsetRate < 0.75) 
				{
					value = calcValue(opacityValue.message3_opacity_out);
					messageValue[3].style.opacity = value;

					value = calcValue(translateYValue.message3_translateY_out);
					messageValue[3].style.transform = `translateY(${value}%)`;
				} 
				else if (yOffsetRate >= 0.75 && yOffsetRate < 0.85) 
				{
					value = calcValue(opacityValue.message3_opacity_in);
					messageValue[3].style.opacity = value;

					value = calcValue(translateYValue.message3_translateY_in);
					messageValue[3].style.transform = `translateY(${value}%)`;
				}

				else 
				{
					value = calcValue(sectionSet[currentSection].imageSettingsValues.image_in);
					sectionSet[currentSection].elemInfo.canvas.style.opacity = value;
				}

				break;

			case 1:		// section1
				break;

			case 2:		// section2
				break;
		}
	};

	// setCanvasImage : ์ด๋ฏธ์ง€๋ฅผ ๋กœ๋”ฉํ•˜๊ณ  ์บ”๋ฒ„์Šค์— ์ด๋ฏธ์ง€๋ฅผ ์…‹ํŒ…ํ•œ๋‹ค.
	//  - parameter : x
	//  - return : x
	const setCanvasImage = function() 
	{
		const imageCount = sectionSet[0].imageSettingsValues.imageCount;
		const imagesArr = sectionSet[0].imageSettingsValues.images;
		let imageElem;

		for(let i = 0; i < imageCount; i++)
		{
			imageElem = new Image();
			imageElem.src = `./image/apple_${i}.png`;

			imagesArr.push(imageElem);
		}

		const ctx = sectionSet[0].elemInfo.canvas.getContext('2d');

		imageElem.addEventListener("load", () => {
			ctx.drawImage(imagesArr[0], 0, 0);
		});
		
	};

	// fixLocalNav : ์Šคํฌ๋กค์„ ๋‚ด๋ฆด๋•Œ global nav ์˜์—ญ์„ ๋ฒ—์–ด๋‚˜๋ฉด position fixed๋กœ ๋ณ€๊ฒฝ
	//  - parameter : x
	//  - return : x
	const fixLocalNav = function()
	{
		if(currentScrollY > 44)
		{
			document.body.id = "fix-local";
		}
		else 
		{
			document.body.removeAttribute("id");
		}
	}

	/////////////////////////////////////////////////////////
	// ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ

	// ๋กœ๋”ฉ๋œํ›„์— ๋ฐœ์ƒ๋˜๋Š” ์ด๋ฒคํŠธ!
	window.addEventListener("load", () => {

		setLayout();

		currentScrollY = window.scrollY;
		currentSection = getCurrentSection();
		sectionYOffset = getSectionYOffset();

		setBodyClass();
		
		setCanvasImage();
	});

	// ์Šคํฌ๋กค์„ ์ง„ํ–‰ํ–ˆ์„์‹œ์— ๋ฐœ์ƒ๋˜๋Š” ์ด๋ฒคํŠธ!
	window.addEventListener("scroll", () => {
		currentScrollY = window.scrollY;
		currentSection = getCurrentSection();
		sectionYOffset = getSectionYOffset();

		setBodyClass();

		playAnimation();

		fixLocalNav();
	});

	// window์ฐฝ์˜ ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•ด์ฃผ์—ˆ์„๋•Œ ๋ฐœ์ƒ๋˜๋Š” ์ด๋ฒคํŠธ!
	window.addEventListener("resize", () => {
		setLayout();

		currentScrollY = window.scrollY;
		currentSection = getCurrentSection();
		sectionYOffset = getSectionYOffset();

		setBodyClass();
	});

	// ๊ธ€์ž์— ๋งˆ์šฐ์Šค๋ฅผ ๋Œ”๋‹ค๊ฐ€ ๋—๋‹ค๊ฐ€ ํ–ˆ์„๋•Œ ๋ฐœ์ƒ๋˜๋Š” ์ด๋ฒคํŠธ!
	(sectionSet[2].elemInfo.message).addEventListener("mouseenter",() => {
		sectionSet[2].elemInfo.message.style.color = "red";
	});

	(sectionSet[2].elemInfo.message).addEventListener("mouseleave",() => {
		sectionSet[2].elemInfo.message.style.color = "white";
	});

 

 


๊ฒฐ๊ณผํ™”๋ฉด

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€