์๋ฐ์คํฌ๋ฆฝํธ : ๋ง์ฐ์ค ํจ๊ณผ : ๋ง์ฐ์ค ๋ฐ๋ผ๋ค๋๊ธฐ
์๋ฐ์คํฌ๋ฆฝํธ์ ๋ค์ํ ํจ๊ณผ๋ค ์ค, ๋ง์ฐ์ค ํจ๊ณผ์ ๋๋ค. ๋ค์ํ ๋ฉ์๋๋ฅผ ํ์ฉํ์ฌ ๋ง์ฐ์ค์ ์์น๊ฐ์ ์ฐพ๊ณ , ์์น๊ฐ์ ๋ฐ๋ผ ์ปค์๊ฐ ์ด๋ํ๊ณ ๋ณํํ๊ฒ๋ ์์ ํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
HTML ์์ค
HTML๋ก๋ ๋จผ์ ๋ง์ฐ์ค์ ์ปค์๋ฅผ ๋ง๋ค์ด ์ฃผ๊ณ , ๋ช ์ธ์ด ๋ค์ด๊ฐ mouse__wrap ๋ฐ์ค, ๋ง์ฐ์ค์ ์์น๊ฐ์ ์์ฑํ mouse__info ๋ฐ์ค๋ฅผ ๋ง๋ค์ด ์ฃผ์์ต๋๋ค.
<section id="mouseType01">
<div class="mouse__cursor"></div>
<div class="mouse__wrap">
<p>The <span class="style1">future</span> depends on <span class="style2">what</span> we do in the <span class="style3">present</span></p>
<p><span class="style4">๋ฏธ๋๋</span> ํ์ฌ <span class="style5">์ฐ๋ฆฌ๊ฐ</span> <span class="style6">๋ฌด์์</span> ํ๋๊ฐ์ ๋ฌ๋ ค์๋ค</p>
</div>
</section>
<div class="mouse__info">
<ul>
<li>clientX : <span class="clientX">0</span>px</li>
<li>clientY : <span class="clientY">0</span>px</li>
<li>offsetX : <span class="offsetX">0</span>px</li>
<li>offsetY : <span class="offsetY">0</span>px</li>
<li>pageX : <span class="pageX">0</span>px</li>
<li>pageY : <span class="pageY">0</span>px</li>
<li>screenX : <span class="screenX">0</span>px</li>
<li>screenY : <span class="screenY">0</span>px</li>
</ul>
</div>
CSS ์์ค
CSS๋ก ์คํ์ผ ์์ฑ์ ์ถ๊ฐํด ์ฃผ์์ต๋๋ค. ๋ช ์ธ์์ ๋ฐ์ค์ด ๊ทธ์ธ ๋จ์ด์ ๊ฐ๊ฐ ๋ง์ฐ์ค๋ฅผ ์ค๋ฒํ๋ฉด ๋ค๋ฅธ ์คํ์ผ๋ก ๋ณํํ๋๋ก ํธ๋์คํผ ํจ๊ณผ๋ฅผ ์ถ๊ฐํด ์ฃผ์์ต๋๋ค.
/* mouseType */
.mouse__wrap {
width: 100%;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
color: #fff;
overflow: hidden;
flex-direction: column;
cursor: none;
}
.mouse__wrap p {
font-size: 2vw;
line-height: 2;
font-weight: 300;
}
.mouse__wrap p:last-child {
font-size: 3vw;
}
.mouse__wrap p span {
border-bottom: 0.35vw dashed rgb(77, 46, 128);
color: rgb(77, 46, 128);
}
@media (max-width: 800px) {
.mouse__wrap p {
font-size: 20px;
padding: 0 20px;
text-align: center;
line-height: 1.5;
word-break: keep-all;
margin-bottom: 10px;
}
.mouse__wrap p:last-child {
font-size: 40px;
}
}
.mouse__cursor {
z-index: 2001;
position: absolute;
left: 0;
top: 0;
width: 50px;
height: 50px;
border-radius: 50%;
border: 3px solid #fff;
background-color: rgba(255, 255, 255, 0.1);
/* ๋ง์ฐ์ค ์ปค์ ์ ํ, ํด๋ฆญ X */
user-select: none;
pointer-events: none;
transition: background-color 0.3s, border-color 0.3s, transform 0.6s, border-radius 0.3s;
}
.mouse__cursor.style1 {
background-color: rgba(255, 0, 0, 0.5);
border-color: red;
}
.mouse__cursor.style2 {
background-color: rgba(255, 166, 0, 0.5);
border-color: orange;
transform: scale(2) rotateY(720deg);
}
.mouse__cursor.style3 {
background-color: rgba(255, 255, 0, 0.5);
border-color: yellow;
transform: scale(1.5) rotateX(720deg);
}
.mouse__cursor.style4 {
background-color: rgba(172, 255, 47, 0.5);
border-color: greenyellow;
transform: scale(10);
}
.mouse__cursor.style5 {
background-color: rgba(0, 0, 255, 0.5);
border-color: blue;
transform: scale(0.1);
}
.mouse__cursor.style6 {
background-color: rgba(137, 43, 226, 0.5);
border-color: blueviolet;
border-radius: 0;
transform: rotate(720deg);
}
.mouse__info {
z-index: 2001;
position: absolute;
left: 20px;
bottom: 10px;
font-size: 14px;
line-height: 1.6;
color: #fff;
}
JAVASCRIPT ์์ค
๋๋ง์ JAVASCIPRT ์์ค์ ๋๋ค. ์ปค์์ ์์น๊ฐ์ ์ผ์ผ์ด ์ ๋ ฅํ ์ ์์ง๋ง, for๋ฌธ๊ณผ forEach๋ฌธ์ผ๋ก๋ ์์ฑํด ๋ณด์์ต๋๋ค.
window.addEventListener("mousemove", (event) => {
document.querySelector(".clientX").innerHTML = event.clientX;
document.querySelector(".clientY").innerHTML = event.clientY;
document.querySelector(".offsetX").innerHTML = event.offsetX;
document.querySelector(".offsetY").innerHTML = event.offsetY;
document.querySelector(".pageX").innerHTML = event.pageX;
document.querySelector(".pageY").innerHTML = event.pageY;
document.querySelector(".screenX").innerHTML = event.screenX;
document.querySelector(".screenY").innerHTML = event.screenY;
});
const cursor = document.querySelector(".mouse__cursor");
window.addEventListener("mousemove", (e) => {
cursor.style.left = e.clientX - 25 + "px"; // ์ปค์์ ์คํ์ผ(์์น๊ฐ) ์ถ๊ฐ
cursor.style.top = e.clientY - 25 + "px";
});
// 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");
// });
//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);
// });
// }
// forEach๋ฌธ
// document.querySelectorAll(".mouse__wrap span").forEach((span, num) => {
// span.addEventListener("mouseover", () => {
// cursor.classList.add("style" + (num + 1));
// });
// span.addEventListener("mouseout", () => {
// cursor.classList.remove("style" + (num + 1));
// });
// });
//getAttribute() ๋ฉ์๋ ํ์ฉํ๊ธฐ : ์์ฑ๊ฐ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ
document.querySelectorAll(".mouse__wrap span").forEach((span) => {
let attr = span.getAttribute("class");
span.addEventListener("mouseover", () => {
cursor.classList.add(attr);
});
span.addEventListener("mouseout", () => {
cursor.classList.remove(attr);
});
});
๊ฒฐ๊ณผ
'Javascript Effect' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์๋ฐ์คํฌ๋ฆฝํธ ํจ๋ด๋์ค ํจ๊ณผ : ์ฌ์ด๋ ๋ฉ๋ด (5) | 2022.09.08 |
---|---|
์๋ฐ์คํฌ๋ฆฝํธ ํจ๋ด๋์ค ํจ๊ณผ : ๋ฉ๋ด ์ด๋ (9) | 2022.09.06 |
์๋ฐ์คํฌ๋ฆฝํธ ์ฌ๋ผ์ด๋ ํจ๊ณผ : ์ข๋ก ์์ง์ด๋ ์ ํ(์ฐ์์ ) (9) | 2022.09.01 |
์๋ฐ์คํฌ๋ฆฝํธ ์ฌ๋ผ์ด๋ ํจ๊ณผ : ์ข๋ก ์์ง์ด๋ ์ ํ (10) | 2022.08.29 |
์๋ฐ์คํฌ๋ฆฝํธ ์ฌ๋ผ์ด๋ ํจ๊ณผ : transition (8) | 2022.08.29 |
๋๊ธ