โญ๏ธ ๋ง์ฐ์ค ์ดํํธ 01 : transform & transition ํจ๊ณผ โญ๏ธ
01. ๋ง์ฐ์ค ํจ๊ณผ ๋ง๋ค์ด๋ณด๊ธฐ : ๋ชฉํ
๐ง ์ ํด์ง ๊ธ์ ์์์ ๋ง์ฐ์ค ์ปค์์ ํํ๊ฐ ๋ณํ๋๋ ํจ๊ณผ๋ฅผ ๋ํ๋ด๋ด
๋๋ค.
๐ง ์ปค์ ๋ชจ์ ๋ณํ ํจ๊ณผ์ ์๋๋ฅผ ์กฐ์ ํด ๋ด
๋๋ค.
[1] ์ปค์ ๋ชจ์ div ๋ง๋ค๊ณ ๋ฌธ์ฅ ์์ฑํ๊ธฐ (html)
section ํ๊ทธ๋ฅผ ๋ง๋ค๊ณ , css ์ script ์ ๊ฒฝ๋ก ์ค์ ์ ์ํด id ๋ฐ class๋ก ์ด๋ฆ์ ์ ํฉ๋๋ค.
๋ฌธ์ฅ์์ ์ ํด์ง ๊ธ์ ์์ ์ปค์๋ฅผ ์ฌ๋ ธ์ ๋, ์ปค์์ ํํ๊ฐ ๋ณํ๋๋๋ก ํ๊ธฐ ์ํด span ํ๊ทธ๋ก ๊ธ์๋ฅผ ๊ฐ์ธ์ค๋๋ค.
span ํ๊ทธ์ css์ script ์ ๊ฒฝ๋ก ์ค์ ์ ์ํด class = style($num) ๋ก ์ด๋ฆ์ ์ง์ด์ค๋๋ค.
___ ์ฐธ๊ณ ํ๊ธฐ ___
`1 a href ์ class ="ํด๋์ค๊ฐ" ๋๋ id ="์์ด๋๊ฐ" ์ ๋ฃ์ด์ฃผ๋ฉด ๋งํฌ ์ด๋์ด ๊ฐ๋ฅํฉ๋๋ค.
<section id="mouseType">
<div class="mouse__cursor"></div>
<div class="mouse__wrap">
<p>The <span class="style1">greatest glory</span> in living lies not in <span
class="style2">never</span> falling, but in
<span class="style3">rising</span> every time we fall. - Nelson Mandela
</p>
<p>์ธ์์์ ๊ฐ์ฅ <span class="style4">ํฐ ์๊ด</span>์ ๋์ด์ง์ง <span class="style5">์๋ ๊ฒ</span>์ ์๋ ๊ฒ์ด ์๋๋ผ ๋งค๋ฒ <span
class="style6">์ผ์ด์ ๋ค</span>๋ ๋ฐ ์๋ค. - ๋ฌ์จ ๋ง๋ธ๋ผ</p>
</div>
</section>
[2] ์ปค์ ๋ชจ์ ๋ฐ ๋ฌธ์ฅ div ๋์์ธํ๊ธฐ (css)
๋ธ๋ผ์ฐ์ ํ๋ฉด์ ์ด๋ป๊ฒ ๋ณด์ฌ์ง๋์ง ๊ณ์ ํ์ธํ๋ฉด์ ๋์์ธํฉ๋๋ค.
/* mouseType 01 */
.mouse__cursor {
position: absolute; /* ๊ณ ์ ํด๋๊ธฐ */
left: 0;
top: 0;
width: 50px; /* ์ปค์ ์์ญ ํฌ๊ธฐ ์ค์ */
height: 50px;
border-radius: 50%;
border: 3px solid #fff; /* ํ
๋๋ฆฌ ์ค์ */
background-color: rgba(225, 225, 225, 0.6);
/* user-select: ํ
์คํธ๋ฅผ ์ ํํ์ ๋์ ๋์์ ์ค์ ํ๋ ์์ฑ -> auto ๋๋ธํด๋ฆญ ๋๋ ๋๋๊ทธ๋ก ์ ํ๋จ, none ํด๋ฆญ ๋ฐ ๋๋๊ทธ๋ก ์ ํ ์๋จ, all ๋จ์ ํด๋ฆญ๋ง์ผ๋ก ์ ํ๋จ */
user-select: none;
/* pointer-events: ๊ทธ๋ํฝ ์์๊ฐ ์ด๋ค ์ํฉ์์ ํฌ์ธํฐ ์ด๋ฒคํธ์ ๋์์ด ๋ ์ ์๋์ง ์ง์ ํจ */
pointer-events: none;
transition:
background-color 0.3s,
border-color 0.3s,
transform 0.6s,
border-radius 0.3s ;
/* all: ๋ชจ๋ ์์ฑ์ ์ ์ฉ, ์ด ์๊ฐ: 1์ด, ์์ ์๊ฐ ์ฐ๊ธฐ: 0.1์ด, ์งํ์๋: ease-in-out = cubic-bezier */
}
.mouse__wrap {
width: 100%; /* 100vw๋ผ๊ณ ์ ์ผ๋ฉด ์คํฌ๋กค๋ฐ๊ฐ ์๊ธฐ๋ฏ๋ก, %๋ก ์
๋ ฅํด์ค */
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column; /* ์์ดํ
์ด ์ธ๋ก ๋ฐฉํฅ์ผ๋ก ํ๋ฆ */
color: #fff;
overflow: hidden; /* ๋์น๋ ๊ฒ ์ ๋ณด์ด๊ฒ ๊ฐ๋ฆฌ๊ธฐ */
cursor: none;
}
.mouse__wrap p {
font-size: 1.5vw;
line-height: 2;
font-weight: 300;
}
/* ๋๋ฒ์งธ(๋ง์ง๋ง) ์ค์๋ง ์ ์ฉ์ํค๊ธฐ ์ํด last-child ์ฌ์ฉ */
.mouse__wrap p:last-child {
font-size: 2vw;
font-weight: 400;
}
.mouse__wrap p span {
border-bottom: 0.15vw dashed #fdf0ba;
color: #fdf0ba;
}
[3] ์ ํด์ง ๋จ์ด ์์์ ๋ณํ๋๋ ์ปค์ ํํ ๋์์ธํ๊ธฐ (css)
๋ธ๋ผ์ฐ์ ํ๋ฉด์ ์ด๋ป๊ฒ ๋ณด์ฌ์ง๋์ง ๊ณ์ ํ์ธํ๋ฉด์ ๋์์ธํฉ๋๋ค.
.mouse__cursor.style1 {
background-color: #ff645088;
border-color: #ff6450;
transform: scale(3) rotateY(720deg);
/* transform: rotate ํ์ , scale ํ๋ ์ถ์, skew ๋นํ๊ธฐ, translate ์ด๋ ๋ฑ์ ํํ ๋ณํ์ ํ๋ ์์ฑ */
}
.mouse__cursor.style2 {
background-color: #b3ff5d81;
border-color: #b3ff5d;
transform: scale(0.5)
}
.mouse__cursor.style3 {
background-color: #6250ff8f;
border-color: #6250ff;
transform: scale(4.5) rotateX(720deg);
}
.mouse__cursor.style4 {
background-color: #7c24cf7e;
border-color: #7c24cf;
transform: scale(3) rotateX(720deg) rotateY(720deg);
}
.mouse__cursor.style5 {
background-color: #fc2d7294;
border-color: #fc2d72;
border-radius: 20%;
transform: scale(7);
}
.mouse__cursor.style6 {
background-color: #ffe55079;
border-color: #ffe550;
border-radius: 5%;
transform: scale(2) rotateX(1080deg) rotateY(75deg);
}
[4] mouse__info ํ์ธ์นธ ๋ง๋ค๊ธฐ (html)
1) div ํ๊ทธ ์์ ๋ชฉ๋ก ul > li ํ๊ทธ๋ฅผ ๋ฃ๊ณ ๋ด์ฉ์ ์์ฑํฉ๋๋ค.
2) ๋์ค์ script๋ฅผ ํตํด ๋ณํํ ๊ฐ์ด ๋ค์ด๊ฐ ์ซ์๋ span ํ๊ทธ๋ก ๊ฐ์ธ์ค๋๋ค.
2) css ์ script ์ ๊ฒฝ๋ก ์ค์ ์ ์ํด, id ์ class ๋ก ์ด๋ฆ์ ์ ํฉ๋๋ค.
<aside id="parallax__info">
<div class="scroll">scrollTop: <span>0</span>px</div>
<div class="info">
<ul>
<li>( scrollTop - section$ ๋์ด๊ฐ ) * 0.07</li>
<li>#(sT - s1) * 0.07 : <span class="offset1">0</span>px</li>
<li>#(sT - s2) * 0.07 : <span class="offset2">0</span>px</li>
<li>#(sT - s3) * 0.07 : <span class="offset3">0</span>px</li>
<li>#(sT - s4) * 0.07 : <span class="offset4">0</span>px</li>
<li>#(sT - s5) * 0.07 : <span class="offset5">0</span>px</li>
<li>#(sT - s6) * 0.07 : <span class="offset6">0</span>px</li>
<li>#(sT - s7) * 0.07 : <span class="offset7">0</span>px</li>
<li>#(sT - s8) * 0.07 : <span class="offset8">0</span>px</li>
<li>#(sT - s9) * 0.07 : <span class="offset9">0</span>px</li>
</ul>
</div>
</aside>
<!-- //parallax__info -->
[5] mouse__info ํ์ธ์นธ ๋์์ธํ๊ธฐ (css)
๋ธ๋ผ์ฐ์ ํ๋ฉด์ ์ด๋ป๊ฒ ๋ณด์ฌ์ง๋์ง ๊ณ์ ํ์ธํ๋ฉด์ ๋์์ธํฉ๋๋ค.
.mouse__info {
position: absolute;
left: 20px;
bottom: 10px;
font-size: 14px;
line-height: 1.6;
color: #fff;
}
[6] ๋ณํ๋ ๊ฐ ๊ณ์ฐํ์ฌ ์ถ๋ ฅํ๊ธฐ (script)
1) ๊ฐ์ ์ด๋ป๊ฒ ๊ฐ์ ธ์์ผ ํ ์ง ์๊ฐํด๋ณด๊ณ , ๊ณ์ฐ ๋ฐฉ๋ฒ์ ๊ตฌ์ํด๋ด
๋๋ค.
2) ๊ฐ์ ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ, ๊ณ์ฐ ๋ฐฉ๋ฒ์ ๋ค์ํ๊ฒ ์๊ฐํด๋ณด๊ณ ์ฌ๋ฌ ๊ฐ์ง๋ก ์ถ๋ ฅํด๋ด
์๋ค.
// mouse__info ๊ฐ ์ถ๋ ฅํ๊ธฐ
window.addEventListener("mousemove", (event) => { // ๋ง์ฐ์ค ์์ง์์ด ์์ ๋, (event ; ์์ง์ด๋ ๋ง์ฐ์ค)๋,
document.querySelector(".clientX").innerText = event.clientX; // ์์ clientX ์์ ์ถ๋ ฅํ์์ค = ์์ง์ด๋ ๋ง์ฐ์ค์ X์ขํ๊ฐ (๋ธ๋ผ์ฐ์ ๊ธฐ์ค)
document.querySelector(".clientY").innerText = event.clientY; // ์์ clientY ์์ ์ถ๋ ฅํ์์ค = ์์ง์ด๋ ๋ง์ฐ์ค์ Y์ขํ๊ฐ (๋ธ๋ผ์ฐ์ ๊ธฐ์ค)
document.querySelector(".offsetX").innerText = event.offsetX; // ์์ offsetX ์์ ์ถ๋ ฅํ์์ค = ์์ง์ด๋ ๋ง์ฐ์ค์ X์ขํ๊ฐ (์์ ๊ธฐ์ค)
document.querySelector(".offsetY").innerText = event.offsetY; // ์์ offsetY ์์ ์ถ๋ ฅํ์์ค = ์์ง์ด๋ ๋ง์ฐ์ค์ Y์ขํ๊ฐ (์์ ๊ธฐ์ค)
document.querySelector(".pageX").innerText = event.pageX; // ์์ pageX ์์ ์ถ๋ ฅํ์์ค = ์์ง์ด๋ ๋ง์ฐ์ค์ X์ขํ๊ฐ (ํ์ด์ง ๊ธฐ์ค)
document.querySelector(".pageY").innerText = event.pageY; // ์์ pageY ์์ ์ถ๋ ฅํ์์ค = ์์ง์ด๋ ๋ง์ฐ์ค์ Y์ขํ๊ฐ (ํ์ด์ง ๊ธฐ์ค)
document.querySelector(".screenX").innerText = event.screenX; // ์์ screenX ์์ ์ถ๋ ฅํ์์ค = ์์ง์ด๋ ๋ง์ฐ์ค์ X์ขํ๊ฐ (๋๋ฐ์ด์ค ๊ธฐ์ค)
document.querySelector(".screenY").innerText = event.screenY; // ์์ screenY ์์ ์ถ๋ ฅํ์์ค = ์์ง์ด๋ ๋ง์ฐ์ค์ Y์ขํ๊ฐ (๋๋ฐ์ด์ค ๊ธฐ์ค)
});
const cursor = document.querySelector(".mouse__cursor"); // ์์ cursot = mouse__cursor ๋ฅผ ์ ์ฅ
// ๋๊ทธ๋ผ๋ฏธ ๋ชจ์์ ์ ๊ฐ์ด๋ฐ์ ์ปค์ ํฌ์ธํธ ๋ง์ถฐ์ฃผ๊ธฐ
window.addEventListener("mousemove", (e) => { // event ๋ฅผ e ๋ผ๊ณ ๋ง์ด ์
cursor.style.left = e.clientX - 25 + "px"; // ๋จ์๋ฅผ ์จ์ค์ผ ํ๋๋ฐ, ์ ์จ์ฃผ๋ฉด ์์ง์ด์ง๋ ์๊ณ ๊ฒ์ฌ์์๋ ์๋ฌ๊ฐ ๋์ค์ง ์์
cursor.style.top = e.clientY - 25 + "px";
});
// ๋ง์ฐ์ค ์ปค์ ์๊น ๋ฐ๊พธ๊ธฐ (css ์ ์ฉ์ํค๊ธฐ) 01. ์ผ์ผ์ด ํ๋์ฉ ๋ถ๋ฌ์ค๋ ๋ฐฉ๋ฒ
// style$์ ๋ํ์ฌ ๋ง์ฐ์ค์ ์์ง์์ด ์์ ๋, cursor์ ํด๋์ค์ style$๋ฅผ ์ถ๊ฐํ์์ค.
// document.querySelector(".style1").addEventListener("mouseover", () => {
// cursor.classList.add("style1");
// });
// document.querySelector(".style1").addEventListener("mouseout", () => {
// cursor.classList.remove("style1");
// });
// document.querySelector(".style2").addEventListener("mouseover", () => {
// cursor.classList.add("style2");
// });
// document.querySelector(".style2").addEventListener("mouseout", () => {
// cursor.classList.remove("style2");
// });
// document.querySelector(".style3").addEventListener("mouseover", () => {
// cursor.classList.add("style3");
// });
// document.querySelector(".style3").addEventListener("mouseout", () => {
// cursor.classList.remove("style3");
// });
// document.querySelector(".style4").addEventListener("mouseover", () => {
// cursor.classList.add("style4");
// });
// document.querySelector(".style4").addEventListener("mouseout", () => {
// cursor.classList.remove("style4");
// });
// document.querySelector(".style5").addEventListener("mouseover", () => {
// cursor.classList.add("style5");
// });
// document.querySelector(".style5").addEventListener("mouseout", () => {
// cursor.classList.remove("style5");
// });
// document.querySelector(".style6").addEventListener("mouseover", () => {
// cursor.classList.add("style6");
// });
// document.querySelector(".style6").addEventListener("mouseout", () => {
// cursor.classList.remove("style6");
// });
// ๋ง์ฐ์ค ์ปค์ ์๊น ๋ฐ๊พธ๊ธฐ (css ์ ์ฉ์ํค๊ธฐ) 02. for๋ฌธ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
// for (let i = 1; i <= 6; i++) {
// document.querySelector(".style" + i).addEventListener("mouseover", () => {
// cursor.classList.add("style" + i);
// });
// document.querySelector(".style" + i).addEventListener("mouseout", () => {
// cursor.classList.remove("style" + i);
// });
// };
// ๋ง์ฐ์ค ์ปค์ ์๊น ๋ฐ๊พธ๊ธฐ (css ์ ์ฉ์ํค๊ธฐ) 03. forEach๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ : mouse__wrap ์ span ๊ธธ์ด๊ฐ ํ์ฉ
// console.log(document.querySelectorAll(".mouse__wrap span").length);
// document.querySelectorAll(".mouse__wrap span").forEach((span, num) => {
// span.addEventListener("mouseover", () => {
// cursor.classList.add("style" + (num +1)); // ๊ธธ์ด๊ฐ์ 0๋ถํฐ ์์ํ๊ธฐ ๋๋ฌธ์ +1 ํด์ค
// });
// span.addEventListener("mouseout", () => {
// cursor.classList.remove("style" + (num +1));
// });
// });
// ๋ง์ฐ์ค ์ปค์ ์๊น ๋ฐ๊พธ๊ธฐ (css ์ ์ฉ์ํค๊ธฐ) 04. getAttribute() : ํด๋น ์์์ ์ง์ ๋ ๊ฐ์ ๋ฐํ
document.querySelectorAll(".mouse__wrap span").forEach(span => {
let attr = span.getAttribute("class"); // ๋ณ์ attr ์ span์ ์์ฑ class ๋ฅผ ์ ์ฅ
span.addEventListener("mouseover", () => {
cursor.classList.add(attr);
});
span.addEventListener("mouseout", () => {
cursor.classList.remove(attr);
});
});
// num.forEach(el => {
// document.querySelector(".style" + el).addEventListener("mouseover", () => {
// cursor.classList.add("style" + el);
// });
// document.querySelector(".style" + el).addEventListener("mouseout", () => {
// cursor.classList.remove("style" + el);
// });
// });
๋๊ธ