๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ดํŽ™ํŠธ

๋งˆ์šฐ์Šค ์ดํŽ™ํŠธ 5

by Youcodein 2022. 9. 29.
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
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€