728x90
반응형
스크립트를 이용한 마우스효과 만들기 01
자바스크립트를 이용하여 마우스 효과를 나타내어 봅시다🖌️
Script
먼저 선택자를 만들어 줍니다.
window.addEventListener("mousemove", (event) => {
document.querySelector(".clientX").textContent = event.clientX;
document.querySelector(".clientY").textContent = event.clientY;
document.querySelector(".offsetX").textContent = event.offsetX;
document.querySelector(".offsetY").textContent = event.offsetY;
document.querySelector(".pageX").textContent = event.pageX;
document.querySelector(".pageY").textContent = event.pageY;
document.querySelector(".screenX").textContent = event.screenX;
document.querySelector(".screenY").textContent = 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문과 forEach문을 이용하여 작성
//for문
for(let i = 1; i <= 6; i++) {
document.querySelector(".style" + i).addEventListener("mouseover", () => {
cursor.classList.add("style" + i)
});
};
for(let i = 1; i <= 6; i++) {
document.querySelector(".style" + i).addEventListener("mouseout", () => {
cursor.classList.remove("style" + i)
});
};
//forEach문
document.querySelectorAll(".mouse__wrap span").forEach((span, i) => {
span.addEventListener("mouseover", () => {
cursor.classList.add("style" + [i+1]);
});
span.addEventListener("mouseout", () => {
cursor.classList.remove("style" + [i+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);
});
});
getAttribute()
getAttribute()를 이용하여 선택한 요소(element)의 특정 속성(attribute)의 값을 가져 올 수 있습니다.
HTML
<section id="mouseType">
<div class="mouse__cursor"></div>
<div class="mouse__wrap">
<p>
All our <span class="style1">dreams</span> can <span class="style2">true</span> if we
have the <span class="style3">courage</span> to pursue them
</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보기
.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: 400;
}
.mouse__wrap p span {
border-bottom: 0.15vw dashed #16213E;
color: #16213E;
}
@media (max-width: 800px) {
.mouse__wrap p {
padding: 0 20px;
font-size: 30px;
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;
transition:
background-color 0.3s,
border-color 0.3s,
transform 0.3s,
border-radius 0.3s
;
}
.mouse__cursor.style1 {
background-color: #80558c67;
border-color: #80558c;
}
.mouse__cursor.style2 {
background-color: #f637ed55;
border-color: #f637ed;
transform: scale(2) rotateY(720deg);
}
.mouse__cursor.style3 {
background-color: #ff64645a;
border-color: #FF6464;
transform: scale(1.5) rotateX(360deg);
}
.mouse__cursor.style4 {
background-color: #f9d92372;
border-color: #F9D923;
transform: scale(4);
}
.mouse__cursor.style5 {
background-color: #cd113a70;
border-color: #CD113B;
transform: scale(0.5);
}
.mouse__cursor.style6 {
background-color: #184d476f;
border-color: #184D47;
transform: scale(5);
border-radius: 5px;
}
.mouse__info {
position: absolute;
left: 20px;
bottom: 10px;
font-size: 14px;
line-height: 1.6;
color: #fff;
}
'Effect' 카테고리의 다른 글
SliderEffect - 04 (3) | 2022.09.19 |
---|---|
mouseEffect 02 (4) | 2022.09.19 |
parallax Effect - 02 (1) | 2022.09.13 |
parallax Effect - 01 (5) | 2022.09.06 |
SliderEffect - 03 (1) | 2022.09.02 |
댓글