728x90
mouse Effect 01
마우스를 hover 했을 때, 지정한 위치에 따라 마우스 커서가 변화하는 이펙트를 공부해보았습니다.
01. HTML
마우스를 올렸을 때 변화하는 글귀를 작성했습니다.
👾 HTML 의 코드 보기
<header id="header" class="arir">
<h1 class="Nix">Javascript Mouse Effect 01</h1>
<p>마우스 이펙트 - 트랜지션 효과</p>
<ul>
<li class="active"><a href="mouseEffect01.html">1</a></li>
<li><a href="mouseEffect02.html">2</a></li>
<li><a href="mouseEffect03.html">3</a></li>
<li><a href="mouseEffect04.html">4</a></li>
<li><a href="mouseEffect05.html">5</a></li>
<li><a href="mouseEffect06.html">6</a></li>
<li><a href="mouseEffect07.html">7</a></li>
</ul>
</header>
<main id="main">
<section id="mouseType">
<div class="mouse__cursor"></div>
<div class="mouse__wrap">
<p class="Nix">Numbing the <span class="style1">pain</span> for a while will <span class="style2">make it worse</span> when you <span class="style3">finally feel it.</span> </p>
<p class="arir"><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>
02. Css
글귀의 위치마다 style을 바꿔, 여러가지 효과가 나오도록 하였습니다.
👾 CSS의 코드 보기
.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: 1.5vw;
line-height: 2;
font-weight: 300;
}
.mouse__wrap p:last-child {
font-size: 1.8vw;
font-weight: 500;
}
.mouse__wrap p span {
border-bottom: 0.05vw solid rgb(219, 73, 146);
color: rgb(219, 73, 146);
}
@media( max-width : 800px){
.mouse__wrap p {
padding: 0 20px;
font-size: 18px;
line-height: 1.5;
text-align: center;
margin-bottom: 10px;
}
.mouse__wrap p:last-child {
font-size: 24px;
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;
transition:
background-color 0.3s,
border-color 0.3s,
transform 0.6s,
rotate 0.3s,
border-radius 0.3s ;
}
.mouse__cursor.style1 {
background-color: rgba(238, 43, 173, 0.199);
border-color: rgb(238, 43, 173);
}
.mouse__cursor.style2 {
background-color: rgba(68, 56, 180, 0.247);
border-color: rgb(68, 56, 180);
transform: scale(2) rotateX(720deg);
}
.mouse__cursor.style3 {
background-color: rgba(252, 217, 60, 0.219);
border-color: rgb(252, 217, 60);
transform: scale(1.5) rotateY(350deg);
}
.mouse__cursor.style4 {
background-color: rgba(94, 201, 250, 0.178);
border-color: rgb(94, 201, 250);
transform: scale(3) rotateX(720deg);
}
.mouse__cursor.style5 {
background-color: rgba(104, 241, 111, 0.192);
border-color: rgb(104, 241, 111);
transform: scale(2) rotate(720deg);
}
.mouse__cursor.style6 {
background-color: rgba(60, 105, 252, 0.171);
border-color: rgb(60, 105, 252);
border-radius: 0;
}
.mouse__info {
position: absolute;
left: 20px;
bottom: 10px;
font-size: 14px;
line-height: 1.6;
color: #fff;
margin-bottom: 10px;
}
03. Javascript
마우스를 움직였을 때, 상대값 절대값 등의 위치 값이 나오도록 하였으며, 감싸진 span의 스타일 값에 따라 마우스를 hover 하였을 때 변화하도록 하였습니다.
👾 Javascript의 코드 보기
//마우스 위치 구하기
window.addEventListener("mousemove",(event) => {
document.querySelector(".clientX").innerText = event.clientX
document.querySelector(".clientY").innerText = event.clientY
document.querySelector(".offsetX").innerText = event.offsetX
document.querySelector(".offsetY").innerText = event.offsetY
document.querySelector(".pageX").innerText = event.pageX
document.querySelector(".pageY").innerText = event.pageY
document.querySelector(".screenX").innerText = event.screenX
document.querySelector(".screenY").innerText = 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";
});
//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' 카테고리의 다른 글
Parallax Effect04 (9) | 2022.09.19 |
---|---|
Slider Effect 04 (8) | 2022.09.19 |
Parallax Effect 03 (3) | 2022.09.12 |
Parallax Effect 02 (10) | 2022.09.08 |
Parallax Effect 01 (6) | 2022.09.06 |
댓글