728x90
๋ฐ์ํ
Javascript Mouse Effect
Mouse ์ดํํธ ์ค GSAP์ ์ด์ฉํ ๋ง์ฐ์ค ๋ฐ๋ผ๋ค๋๊ธฐ ํจ๊ณผ์ ๋๋ค.
HTML
html ์์ค์ ๋๋ค.
<main id="main">
<section id="mouseType">
<div class="mouse__cursor"></div>
<div class="mouse__cursor2"></div>
<div class="mouse__wrap">
<p>๋ฉ์๋ ๋ช
์ธ<span class="style1">hoc</span> <span class="style2">quoque</span> <span
class="style3">transibit</span>๋ฉ์๋ ๋ช
์ธ</p>
<p>๋ฉ์๋ ๋ช
์ธ<span class="style4">์ด</span> <span class="style5">๋ํ</span> <span
class="style6">์ง๋๊ฐ๋ฆฌ๋ผ.</span>๋ฉ์๋ ๋ช
์ธ</p>
</div>
</section>
</main>
CSS
css ์์ค์ ๋๋ค.
/* MouseType */
.mouse__wrap {
width: 100%;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
color: #fff;
overflow: hidden;
cursor: none;
}
.mouse__wrap p {
font-size: 2vw;
line-height: 2;
font-weight: normal;
}
.mouse__wrap p:last-child {
font-size: 3vw;
font-weight: 400;
}
.mouse__wrap p span {
border-bottom: 1px solid #ff9aa1;
color: #ff9aa1;
}
@media (max-width: 800px) {
.mouse__wrap p {
padding: 0 20px;
font-size: 20px;
line-height: 1.5;
text-align: center;
margin: 10px;
}
.mouse__wrap p:last-child {
font-size: 40px;
line-height: 1.5;
text-align: center;
word-break: keep-all;
}
}
.mouse__cursor {
position: absolute;
left: 0;
top: 0;
width: 10px;
height: 10px;
z-index: 10002;
border-radius: 50%;
background: rgba(255,255,255,0.1);
user-select: none;
pointer-events: none;
transition: transform 0.3s, opacity 0.2s;
}
.mouse__cursor2 {
position: absolute;
left: 0;
top: 0;
width: 30px;
height: 30px;
z-index: 10001;
border-radius: 50%;
background: rgba(255,255,255,0.3);
user-select: none;
pointer-events: none;
transition: transform 0.3s, opacity 0.2s;
}
.mouse__cursor.active {
transform: scale(0);
}
.mouse__cursor2.active {
transform: scale(5);
background: rgba(255,166,0,0.6);
}
.mouse__cursor.active2 {
transform: scale(0);
}
.mouse__cursor2.active2 {
transform: scale(3);
background: rgba(255, 161, 161, 0.541);
}
.mouse__cursor.active3 {
transform: scale(0);
}
.mouse__cursor2.active3 {
transform: scale(3);
background: rgba(142, 223, 255, 0.6);
}
Javascript
Javascript ์์ค์ ๋๋ค.
const cursor = document.querySelector(".mouse__cursor");
const cursor2 = document.querySelector(".mouse__cursor2");
const span = document.querySelectorAll(".mouse__wrap span");
const span2 = document.querySelectorAll("#header ul li");
const span3 = document.querySelectorAll(".modal__wrap .modal__btn");
window.addEventListener("mousemove" , e => {
// ์ปค์ ์ขํ๊ฐ ํ ๋น
// cursor.style.left = e.pageX -5 + "px";
// cursor.style.top = e.pageY -5 + "px";
// cursor2.style.left = e.pageX -15 + "px";
// cursor2.style.top = e.pageY -15 + "px";
// GSAP
gsap.to(cursor, {duration: 0.3, left: e.pageX -5, top: e.pageY -5})
gsap.to(cursor2, {duration: 0.8, left: e.pageX -15, top: e.pageY -15})
// ์ค๋ฒ ํจ๊ณผ
// mouseenter / mouseover / ์ด๋ฒคํธ ๋ฒ๋ธ๋ง
// (ํ์ดํํจ์๋ฅผ ์ฐ๋ฉด this ๋ฅผ ์ฌ์ฉํ ์ ์๋ค!!)
span.forEach(span => {
span.addEventListener("mouseenter", ()=>{
cursor.classList.add("active");
cursor2.classList.add("active");
});
span.addEventListener("mouseleave", ()=>{
cursor.classList.remove("active");
cursor2.classList.remove("active");
});
});
span2.forEach(span2 => {
span2.addEventListener("mouseenter", ()=>{
cursor.classList.add("active2");
cursor2.classList.add("active2");
});
span2.addEventListener("mouseleave", ()=>{
cursor.classList.remove("active2");
cursor2.classList.remove("active2");
});
});
span3.forEach(span3 => {
span3.addEventListener("mouseenter", ()=>{
cursor.classList.add("active3");
cursor2.classList.add("active3");
});
span3.addEventListener("mouseleave", ()=>{
cursor.classList.remove("active3");
cursor2.classList.remove("active3");
});
});
});
728x90
๋ฐ์ํ
'์ดํํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํจ๋ด๋์ค ์ดํํธ 5. ์ด์ง๊ฐ ํจ๊ณผ (์งํ์ค) (2) | 2022.09.20 |
---|---|
์ฌ๋ผ์ด๋ ์ดํํธ 4. ๋ฒํผ ๋๋ฌ ๋๊ธฐ๊ธฐ (5) | 2022.09.19 |
ํจ๋ด๋์ค ์ดํํธ 4. ๋ด์ฉ ๋ํ๋๊ธฐ (3) | 2022.09.19 |
ํจ๋ด๋์ค ์ดํํธ 3. ๋ฉ๋ด ๋ํ๋๊ธฐ / ํ๋ฒํผ (4) | 2022.09.19 |
ํจ๋ด๋ญ์ค ์ดํํธ - 02. ์ฌ์ด๋ ๋ฉ๋ด (1) | 2022.09.13 |
๋๊ธ