๊ตญ๋น์ง์ 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";
});
๊ฒฐ๊ณผํ๋ฉด
๋ฐ์ํ
๋๊ธ