728x90
반응형
스크립트를 이용한 마우스효과 만들기 04
이번에는 마우스를 올렸을때 이미지가 움직이는 효과를 만들어 보도록 합시다~
Javascript
const cursor = document.querySelector(".mouse__cursor");
const cursorRect = cursor.getBoundingClientRect();
document.querySelector(".mouse__wrap figure").addEventListener("mousemove", (e) => {
// 커서
gsap.to(cursor, {
duration: .4,
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/20 +"px,"+ centerPageY/20 +"px)";
gsap.to(imgMove, {
duration: 0.4,
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;
});
gsap.to(cursor, {
duration: .4,
left : e.pageX - cursorRect.width/2,
top : e.pageY - cursorRect.height/2
});
선택자를 만든 다음 gsap를 이용하여 마우스 커서가 부드럽게 움직이는 애니메이션을 만들어 주도록 합니다.
//마우스 좌표 값 가운데로 초기화
//전체 길이/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/20 +"px,"+ centerPageY/20 +"px)";
gsap.to(imgMove, {
duration: 0.4,
x : centerPageX/20,
y : centerPageY/20
})
마우스의 좌표갑을 가운데로 초기화를 시키기 위해 전체 길이/2 - 마우스 좌표값 == 0를 해줍니다.
💦이제 이미지를 움직이게 하는데 이때, 마우스의 좌표값을 가운데로 초기화 시켜 주지 않으면 이미지가 움직이지 않기때문에 마우스의
좌표값을 가운데로 초기화 시켜주도록 합니다.
x : centerPageX/20, y : centerPageY/20 값을 나누기 20을 해주면 이미지가 많이 튀어나가지 않고, 스무스하게 움직이는걸 볼 수있습니다.
HTML
<main id="main">
<section id="mouseType">
<div class="mouse__cursor"></div>
<div class="mouse__wrap">
<figure>
<img src="../assets/img/effect_bg11.jpg" alt="이미지">
<figcaption>
<p>Life is either a great adventure or nothing.</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;
}
.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; /* background cover와 같은 효과, 이미지에 줄수 있음 */
}
.mouse__wrap figure figcaption {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
.mouse__wrap figure p {
font-size: 20px;
white-space: nowrap;
line-height: 1.5;
font-weight: 400;
}
.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;
}
object-fit 속성은 대체 요소의 콘텐츠의 크기를 조절할 수 있습니다. object-fit:cover는 background cover와 같은 효과를 줄 수 있습니다.
이미지의 width값과 height값을 110%로 작성하여 이미지가 넘치게 해줍니다.
'Effect' 카테고리의 다른 글
searchEffect - 04 (4) | 2022.09.29 |
---|---|
mouseEffect - 05 (2) | 2022.09.29 |
mouseEffect - 03 (1) | 2022.09.22 |
parallax Effect - 05 (2) | 2022.09.20 |
parallax Effect - 04 (2) | 2022.09.20 |
댓글