์๋ฐ์คํฌ๋ฆฝํธ : ํจ๋ด๋์ค ํจ๊ณผ : ํ ์คํธ ํจ๊ณผ
์๋ฐ์คํฌ๋ฆฝํธ์ ๋ค์ํ ํจ๊ณผ๋ค ์ค, ํจ๋ด๋์ค ํจ๊ณผ์ ๋๋ค. ์ด๋ฒ์๋ ์คํฌ๋กค์ ๋ด๋ฆด ๋๋ง๋ค ํด๋นํ๋ ์น์ ์ ํ ์คํธ๊ฐ ํ ๊ธ์์ฉ ๋ํ๋๋๋ก ์์ ํด ๋ณด๊ฒ ์ต๋๋ค.
1. ๊ธ์จ ์ชผ๊ฐ๊ธฐ
document.querySelectorAll(".split").forEach((text) => {
let splitText = text.innerText;
let splitWrap = splitText.split("").join("</span><span aria-hidden='true'>");
splitWrap = "<span aria-hidden='true'>" + splitWrap + "</span>";
text.innerHTML = splitWrap;
// p ํ๊ทธ(split)์ label ์์ฑ ์ถ๊ฐ
text.setAttribute("aria-label", splitText);
});
๊ธ์๊ฐ ํ๋์ฉ ๋ํ๋๋๋ก ํ๋ ค๋ฉด, ๋ชจ๋ ๊ธ์๊ฐ ํ ๊ธ์์ฉ span ํ๊ทธ์ ๋ด๊ฒจ ์์ด์ผ
ํฉ๋๋ค. 9๊ฐ์ ์น์
์ ๊ธ์๋ฅผ ๋ชจ๋ ์ง์ HTML๋ก ์ชผ๊ฐค ์๋ ์์ผ๋, ์คํฌ๋ฆฝํธ๋ก
์์
ํด ์ค๋๋ค.
split์ ๊ฐ ์น์
์ ํ
์คํธ ๋ฐ์ค์
๋๋ค. ์น์
๋ง๋ค ํ
์คํธ ๋ฐ์ค๋
์กด์ฌํ๋ฏ๋ก, ์ด 9๊ฐ์ split์ด ์กด์ฌํ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ querySelectorAll์ ์ฌ์ฉํ์ฌ
์ ํํด ์ค๋๋ค. splitText ๋ณ์๋ ๊ฐ๊ฐ์ split์ ์ ์ฉํ๋ค๋ ์๋ฏธ์
๋๋ค. ๊ฐ๊ฐ์
split ๋ฐ์ค์ split(), join() ๋ฉ์๋๋ฅผ ์ฐจ๋ก๋ก ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค.
์ฌ๊ธฐ์์๋ ์ ๊ทผ์ฑ ํฅ์์ ์ํด, split(p ํ๊ทธ)์ ์ ์ฒด ๋ฌธ์ฅ์ ์์ฑํ aria-label์
์ถ๊ฐํด ์ฃผ๊ณ , ํ ๊ธ์์ฉ์ ํด๋นํ๋ ๊ฐ๊ฐ์ span์ aria-hidden='true'๋ฅผ ์ถ๊ฐํด
์ฃผ์์ต๋๋ค.
split() ๋ฉ์๋๋ ๋ฌธ์์ด์์ ์ํ๋ ๊ฐ์ ์ถ์ถํ์ฌ ๋ฐฐ์ด๋ก ๋ฐํํฉ๋๋ค. join()
๋ฉ์๋๋ ๋ฐฐ์ด์ ๋ชจ๋ ์์๋ฅผ ์ฐ๊ฒฐํด ํ๋์ ๋ฌธ์์ด๋ก ๋ง๋ญ๋๋ค.
"์๋
ํ์ธ์"๋ผ๋ ๊ธ์๊ฐ ์๋ค๊ณ ๊ฐ์ ํ์ ๋, split('') ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด
"์","๋
","ํ","์ธ","์"๊ฐ ๋ฉ๋๋ค. ๊ทธ ์ดํ join("<span></span>")
๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด
"์<span></span>๋
<span></span>ํ<span></span>์ธ<span></span>์"๊ฐ
๋ฉ๋๋ค.
2. scroll ์ด๋ฒคํธ ๋ง๋ค์ด ์ฃผ๊ธฐ
function scroll() {
let scrollTop = window.scrollY;
document.querySelector("#parallax__info .scroll").innerText = Math.round(scrollTop);
requestAnimationFrame(scroll);
}
scroll();
๋จผ์ , scroll ์ด๋ฒคํธ๋ฅผ ๋ง๋ค์ด ์ค๋๋ค. scroll ์ด๋ฒคํธ๋ requestAnimationFrame์ผ๋ก
์์
ํด ์ฃผ๋ ๊ฒ์ด ๋ฒ๋ฒ
๊ฑฐ๋ฆผ์ ์ต์ํํ ์ ์์ต๋๋ค. scrollTop์ ๊ฐ์ scrollY๋ก
๊ฐ๋จํ๊ฒ ๊ตฌํ ์ ์์ต๋๋ค. ์น์ ํ๊ฒ info ๋ฐ์ค์๋ ์ถ๋ ฅํด ์ค์๋ค.
์ ๋๋ฉ์ด์
ํจ๊ณผ๋ CSS๋ก๋, JAVASCRIPT๋ก๋ ์ค ์ ์์ต๋๋ค.
2-1) CSS๋ก ํจ๊ณผ ์ฃผ๊ธฐ
document.querySelectorAll(".content__item").forEach((item) => {
if (scrollTop >= item.offsetTop - 500) {
item.querySelector(".split").classList.add("show");
}
});
content__item์ split์ ๋ถ๋ชจ ๋ฐ์ค์ ๋๋ค. split์ ์ง์ ์ ํํ๋ฉด ๋ชจ๋ ํ ์คํธ๊ฐ ๋์์ ๋ํ๋๊ฒ ๋๋ฏ๋ก, ์์ ๋ฐ์ค๋ฅผ ์ ํํ๊ณ , if๋ฌธ ์์์ split์ ์ ํํด ์ค์๋ค. ๊ฐ ์น์ ์ scroll์ด ์์นํ์ ๋, split์ show ํด๋์ค๋ฅผ ์ถ๊ฐํด ์ฃผ๋ฉด ๋๋ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ด์ง๋ง, ์๋ ์์ค์ฒ๋ผ CSS๋ก span์ ๊ฐ๊ฐ transition-delay๋ฅผ ์ง์ ํด ์ฃผ์ด์ผ ํฉ๋๋ค... ํ๋ญ๋๋ค.
๋ ธ๊ฐ๋ค์ ํ์
.split span {
opacity: 0;
transform: translateY(50px);
transition: all 0.3s cubic-bezier(0, 0.71, 0.11, 1.66);
/* transform์ block ๊ตฌ์กฐ๋ง ๋จนํ๋ค! */
display: inline-block;
/* block ๊ตฌ์กฐ๋ก ๋ฐ๊พธ๋ฉด ๋์ด์ฐ๊ธฐ๊ฐ ์ฌ๋ผ์ง๋ฏ๋ก, ์ต์ ๋๋น๋ฅผ ์ง์ ํด์ฃผ๊ธฐ */
min-width: 1vw;
}
.split.show span {
opacity: 1;
transform: translateY(0);
}
.split span:nth-child(1) {
transition-delay: 100ms;
}
.split span:nth-child(2) {
transition-delay: 150ms;
}
.split span:nth-child(3) {
transition-delay: 200ms;
}
.split span:nth-child(4) {
transition-delay: 250ms;
}
.split span:nth-child(5) {
transition-delay: 300ms;
}
.split span:nth-child(6) {
transition-delay: 350ms;
}
.split span:nth-child(7) {
transition-delay: 400ms;
}
.split span:nth-child(8) {
transition-delay: 450ms;
}
.split span:nth-child(9) {
transition-delay: 500ms;
}
.split span:nth-child(10) {
transition-delay: 550ms;
}
.split span:nth-child(11) {
transition-delay: 600ms;
}
.split span:nth-child(12) {
transition-delay: 650ms;
}
.split span:nth-child(13) {
transition-delay: 700ms;
}
.split span:nth-child(14) {
transition-delay: 750ms;
}
.split span:nth-child(15) {
transition-delay: 800ms;
}
.split span:nth-child(16) {
transition-delay: 850ms;
}
.split span:nth-child(17) {
transition-delay: 900ms;
}
.split span:nth-child(18) {
transition-delay: 950ms;
}
.split span:nth-child(19) {
transition-delay: 1000ms;
}
.split span:nth-child(20) {
transition-delay: 1050ms;
}
.split span:nth-child(21) {
transition-delay: 1100ms;
}
.split span:nth-child(22) {
transition-delay: 1150ms;
}
.split span:nth-child(23) {
transition-delay: 1200ms;
}
.split span:nth-child(24) {
transition-delay: 1250ms;
}
.split span:nth-child(25) {
transition-delay: 1300ms;
}
.split span:nth-child(26) {
transition-delay: 1350ms;
}
.split span:nth-child(27) {
transition-delay: 1400ms;
}
.split span:nth-child(28) {
transition-delay: 1450ms;
}
.split span:nth-child(29) {
transition-delay: 1500ms;
}
.split span:nth-child(30) {
transition-delay: 1550ms;
}
.split span:nth-child(31) {
transition-delay: 1600ms;
}
.split span:nth-child(32) {
transition-delay: 1650ms;
}
.split span:nth-child(33) {
transition-delay: 1700ms;
}
.split span:nth-child(34) {
transition-delay: 1750ms;
}
.split span:nth-child(35) {
transition-delay: 1800ms;
}
.split span:nth-child(36) {
transition-delay: 1850ms;
}
.split span:nth-child(37) {
transition-delay: 1900ms;
}
.split span:nth-child(38) {
transition-delay: 1950ms;
}
.split span:nth-child(39) {
transition-delay: 2000ms;
}
.split span:nth-child(40) {
transition-delay: 2050ms;
}
.split span:nth-child(41) {
transition-delay: 2100ms;
}
.split span:nth-child(42) {
transition-delay: 2150ms;
}
.split span:nth-child(43) {
transition-delay: 2200ms;
}
.split span:nth-child(44) {
transition-delay: 2250ms;
}
2-2) JAVASCRIPT๋ก ํจ๊ณผ ์ฃผ๊ธฐ
document.querySelectorAll(".content__item").forEach((item) => {
if (scrollTop >= item.offsetTop - 500) {
item.querySelector(".split").classList.add("show");
item.querySelectorAll(".split span").forEach((span, index) => {
span.style.transitionDelay = 100 + 50 * index + "ms";
});
}
});
์ด๋ฒ์๋ JAVASCRIPT์
๋๋ค. forEach๋ฅผ ์ฌ์ฉํ์ฌ ํจ์ฌ ๋ ๊ฐ๋จํ๊ฒ ์์
ํ ์
์์ต๋๋ค.
๋จผ์ split์ show ํด๋์ค๋ฅผ ์ถ๊ฐํด ์ฃผ๊ณ , ๊ฐ๊ฐ์ span์ forEach๋ฌธ์ ์ฌ์ฉํด ์ค๋๋ค.
transitionDelay์ ์ด๊ธฐ๊ฐ์ 100ms๋ก ์ง์ ํ๊ณ , index๋ฅผ ๊ณฑํด์ฃผ์ด 50ms์ฉ ์ฆ๊ฐํ๊ฒ
์์
ํด ์ฃผ๋ฉด ์์ฑ์
๋๋ค.
๊ฒฐ๊ณผ
'Javascript Effect' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์๋ฐ์คํฌ๋ฆฝํธ ํจ๋ด๋์ค ํจ๊ณผ : ๋ฆฌ๋น ํจ๊ณผ (4) | 2022.10.09 |
---|---|
์๋ฐ์คํฌ๋ฆฝํธ ๊ฒ์ ํจ๊ณผ : filter() (4) | 2022.09.29 |
์๋ฐ์คํฌ๋ฆฝํธ ๋ง์ฐ์ค ํจ๊ณผ : ํ ์คํธ ํจ๊ณผ (5) | 2022.09.29 |
์๋ฐ์คํฌ๋ฆฝํธ ๊ฒ์ ํจ๊ณผ : find() (5) | 2022.09.28 |
์๋ฐ์คํฌ๋ฆฝํธ ๋ง์ฐ์ค ํจ๊ณผ : ๊ธฐ์ธ๊ธฐ ํจ๊ณผ / ๋ฐ์ ํจ๊ณผ (10) | 2022.09.28 |
๋๊ธ