728x90
๋ฐ์ํ
Javascript Mouse Effect
Mouse ์ดํํธ ์ค ๋ง์ฐ์ค ์ปค์๋ฅผ ์์ง์ด๋ฉด ์ค์์ ์ฌ์ง์ด ์์ง์ด๋ ํจ๊ณผ์ ๋๋ค.
ํต์ฌ ํฌ์ธํธ(์งํ์ค)
1. ์งํ์ค
HTML
html ์์ค์ ๋๋ค.
<main id="main">
<section id="mouseType" >
<div class="mouse__cursor"></div>
<div class="mouse__wrap">
<figure>
<img src="img/effect_bg06-min.jpg" alt="์ด๋ฏธ์ง">
<figcaption>
<p>The way to get started is to quit talking and begin doing.</p>
<p>๋ฌด์ธ๊ฐ๋ฅผ ์์ํ๋ ค๋ฉด ๋ง์ ๋ฉ์ถ๊ณ ํ๋ํด์ผ ํ๋ค.</p>
</figcaption>
</figure>
</div>
</section>
<div class="mouse__info">
<ul>
<li>mousePageX : <span class="mousePageX">0</span>px</li>
<li>mousePageY : <span class="mousePageY">0</span>px</li>
<li>centerPageX : <span class="centerPageX">0</span>px</li>
<li>centerPageY : <span class="centerPageY">0</span>px</li>
</ul>
</div>
</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 figure{
width: 50vw;
height: 50vh;
position: relative;
overflow: hidden;
transition: transform 500ms ease;
cursor: none;
}
.mouse__wrap figure:hover {
transform: scale(1.025);
}
.mouse__wrap figure img {
position: absolute;
left: -5%;
top: -5%;
width: 110%;
height: 110%;
object-fit: cover;
}
figcaption {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
font-size: 16px;
white-space: nowrap;
line-height: 1.4;
font-weight: 300;
}
.mouse__cursor {
position: absolute;
left: 0;
top: 0;
width: 20px;
height: 20px;
background: #fff;
border-radius: 50%;
z-index: 1000;
user-select: none;
pointer-events: none;
}
.mouse__info {
position: absolute;
left: 20px;
bottom: 10px;
font-size: 14px;
line-height: 1.6;
color: #fff;
}
Javascript
Javascript ์์ค์ ๋๋ค.
const cursor = document.querySelector(".mouse__cursor");
const cursorRect = cursor.getBoundingClientRect();
document.querySelector(".mouse__wrap figure").addEventListener("mousemove",(e) => {
//์ปค์
gsap.to(cursor, {
duration: .2,
left: e.pageX -cursorRect.width/2,
top: e.pageY -cursorRect.height/2,
});
//๋ง์ฐ์ค ์ขํ๊ฐ
let mousePageX = e.pageX;
let mousePageY = e.pageY;
//์ ์ฒด ๊ฐ๋ก๊ฐ
// window.innerWidth //1920 : ๋ธ๋ผ์ฐ์ ํฌ๊ธฐ
// window.outerWidth //1920 : ๋ธ๋ผ์ฐ์ ํฌ๊ธฐ(์คํฌ๋กค๋ฐ ํฌํจ)
// window.screen.Width //1920 : ํ๋ฉด ํฌ๊ธฐ
//๋ง์ฐ์ค ์ขํ ๊ฐ ๊ฐ์ด๋ฐ๋ก ์ด๊ธฐํ
//์ ์ฒด ๊ธธ์ด/2 - ๋ง์ฐ์ค ์ขํ๊ฐ == 0
let centerPageX = window.innerWidth/2 - mousePageX;
let centerPageY = window.innerHeight/2 - mousePageY;
//์ด๋ฏธ์ง ์์ง์ด๊ธฐ
const imgMove = document.querySelector(".mouse__wrap figure img");
// imgMove.style.transform = "translate(" + centerPageX + "px, " + + "px)";
gsap.to(imgMove,{
duration: 0.3,
x: centerPageX/20,
y: centerPageY/20,
})
//์ถ๋ ฅ
document.querySelector(".mousePageX").textContent = mousePageX;
document.querySelector(".mousePageY").textContent = mousePageY;
document.querySelector(".centerPageX").textContent = centerPageX;
document.querySelector(".centerPageY").textContent = centerPageY;
});
728x90
๋ฐ์ํ
'์ดํํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์์น ์ดํํธ 4 (2) | 2022.09.29 |
---|---|
๋ง์ฐ์ค ์ดํํธ 4. ์์ง์ด๋ ๊ทธ๋ฆผ (3) | 2022.09.23 |
๋ง์ฐ์ค ์ดํํธ 3. ์กฐ๋ช ํจ๊ณผ(์งํ์ค) (1) | 2022.09.22 |
ํจ๋ด๋์ค ์ดํํธ 5. ์ด์ง๊ฐ ํจ๊ณผ (์งํ์ค) (2) | 2022.09.20 |
์ฌ๋ผ์ด๋ ์ดํํธ 4. ๋ฒํผ ๋๋ฌ ๋๊ธฐ๊ธฐ (5) | 2022.09.19 |
๋๊ธ