본문 바로가기
이펙트 만들기/마우스 이펙트 만들기

마우스 이펙트 효과 04

by 코딩달림 2022. 9. 23.
728x90

마우스 이펙트

자바스크립트를 이용한 마우스 효과 만들기 입니다.


마우스 이펙트 04 - 이미지 효과

마우스가 이미지 안에서 움직일 때 이미지가 살짝 커지며 이동하는 효과입니다.


1. 이미지 세팅

가운데에 이미지를 하나 넣고 마우스 커서가 될 원 하나를 만드는 간단한 작업입니다.

소스보기
▶ CSS 파트
/* mouseType */
#mouseType {}
.mouse__cursor {}
.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;  /* 백그라운드 사이즈에 맞추는 기능*/
}
.mouse__wrap figure figcaption{
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    font-size: 20px;
    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;
}


▶ HTML 파트

<section id="mouseType">
<div class="mouse__wrap">
    <div class="mouse__cursor"></div>
    <figure>
        <img src="../assets/img/effect_bg04-min.jpg" alt="이미지">

        <figcaption>
            <p>If you can dream it, you can do it</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>

이미 한번씩 했던 간단한 작업이라 설명할게 없습니다. 이번엔 마우스 좌표값을 표시했습니다.


2. 스크립트 파트

커서를 원 중앙에 고정시킨 후 마우스 중앙 좌표값을 초기화 합니다.
이러한 이유는 마우스가 이미지 내에서 이동 시 중앙을 중심으로 이동할 수 있도록 하기 위함입니다.

소스보기
▶ 자바스크립트 파트
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/20 +", "+ centerPageY/20 +")";

    gsap.to(imgMove, {
        duration: 0.2,
        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;
});

mousePage로 마우스의 가로값과 세로 값을 구하고 window.innerWidth와 window.innerHeight로 브라우저의 크기를 구합니다.
마우스 중앙 좌표값을 브라우저 절반값으로 뺄 시 마우스의 중앙 좌표값을 0으로 초기화가 가능합니다.

이 후 addEventListener("mousemove")를 통해 마우스가 이동할 때마다 이미지가 움직이도록 설정하면 이미지가 중앙을 중심으로 해서 이동하게 됩니다.

'이펙트 만들기 > 마우스 이펙트 만들기' 카테고리의 다른 글

마우스 이펙트 05  (4) 2022.09.28
마우스 이펙트 03  (3) 2022.09.22
마우스 이펙트 02  (0) 2022.09.22
마우스 이펙트 만들기 01  (2) 2022.09.06

댓글


광고 준비중입니다.