728x90
반응형
마우스 이펙트 - 마우스 따라다니기
포인터의 좌표값에 따라서 마우스의 움직임과 효과가 있는 마우스 이펙트를 만들어 봅시다.
html
<main id="main">
<section id="mouseType">
<div class="mouse__cursor"></div>
<div class="mouse__wrap">
<p><span class="style1">Only</span> those who dare to <span class="style2">fail</span> greatly can ever
achieve <span class="style3">greatly</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>
</main>
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: 300;
}
.mouse__wrap p:last-child {
font-size: 3vw;
font-weight: 300;
}
.mouse__wrap p span {
border-bottom: 0.15vw dashed blue;
color: blue;
}
@media (max-width: 800px) {
.mouse__wrap p {
padding: 0 20px;
font-size: 24px;
line-height: 1.5;
text-align: center;
margin-bottom: 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: 50px;
height: 50px;
border-radius: 50%;
border: 3px solid #fff;
background-color: rgba(255, 255, 255, 0.1);
user-select: none;
pointer-events: none;
perspective: 400px;
transform-style: preserve-3d;
transition:
background-color 0.3s,
border-color 0.3s,
transform 0.6s,
border-radius 0.3s;
}
.mouse__cursor.style1 {
background-color: rgba(68, 124, 184, 0.476);
border-color: rgb(38, 66, 115);
}
.mouse__cursor.style2 {
background-color: rgba(68, 176, 184, 0.499);
border-color: rgb(38, 91, 115);
transform: scale(2) rotateY(720deg);
}
.mouse__cursor.style3 {
background-color: rgba(68, 184, 182, 0.47);
border-color: rgb(38, 115, 98);
transform: scale(1.5) rotateX(545deg);
}
.mouse__cursor.style4 {
background-color: rgba(165, 68, 184, 0.593);
border-color: rgb(102, 38, 115);
transform: scale(10);
}
.mouse__cursor.style5 {
background-color: rgba(184, 68, 113, 0.406);
border-color: rgb(115, 38, 82);
transform: scale(0.5);
}
.mouse__cursor.style6 {
background-color: rgba(111, 184, 68, 0.466);
border-color: rgb(48, 115, 38);
transform: scale(5) skew(40deg);
}
.mouse__info {
position: absolute;
left: 20px;
bottom: 10px;
font-size: 14px;
line-height: 1.6;
color: #fff;
}
js
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"; //js는 left, top의 단위를 적어야함.
});
// 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);
});
});
728x90
반응형
'Effect' 카테고리의 다른 글
parallaxEffect02 (4) | 2022.09.08 |
---|---|
ParallaxEffect01 (11) | 2022.09.06 |
sliderEffect03 (4) | 2022.09.01 |
sliderEffect01 (7) | 2022.08.29 |
sliderEffect02 (8) | 2022.08.29 |