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

๋งˆ์šฐ์Šค ์ดํŽ™ํŠธ 3. ์กฐ๋ช… ํšจ๊ณผ(์ง„ํ–‰์ค‘)

by Youcodein 2022. 9. 22.
728x90
๋ฐ˜์‘ํ˜•

Javascript Mouse Effect

Mouse ์ดํŽ™ํŠธ ์ค‘ ๋งˆ์šฐ์Šค ์ปค์„œ๋ฅผ ์›€์ง์ด๋ฉด ์› ์•ˆ์— ์ˆจ๊ฒจ์ง„ ์‚ฌ์ง„์ด ๋ณด์ด๋Š” ํšจ๊ณผ์ž…๋‹ˆ๋‹ค.

ํ•ต์‹ฌ ํฌ์ธํŠธ(์ง„ํ–‰์ค‘)

 

1. ์ง„ํ–‰์ค‘

HTML

html ์†Œ์Šค์ž…๋‹ˆ๋‹ค.

<main id="main">
    <section id="mouseType">
        <div class="mouse__cursor"></div>
        <div class="mouse__cursor2"></div>

        <div class="mouse__wrap">
            <p>Seize the day!</p>
            <p>์˜ค๋Š˜์„ ์ฆ๊ธฐ์„ธ์š”!</p>
        </div>
    </section>

</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 p {
    font-size: 2vw;
    line-height: 2;
    font-weight: normal;
}

.mouse__wrap p:last-child {
    font-size: 3vw;
    font-weight: 400;
}

.mouse__wrap p span {
    border-bottom: 1px solid #ff9aa1;
    color: #ff9aa1;
}

@media (max-width: 800px) {
    .mouse__wrap p {
        padding: 0 20px;
        font-size: 20px;
        line-height: 1.5;
        text-align: center;
        margin: 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: 200px;
    height: 200px;
    z-index: -1;
    border-radius: 50%;
    background-image: url(img/effect_bg02-min.jpg);
    background-size: cover;
    background-position: center center;
    background-attachment: fixed;
    border: 5px solid #fff;
}

Javascript

Javascript ์†Œ์Šค์ž…๋‹ˆ๋‹ค.

const cursor = document.querySelector(".mouse__cursor");

const circle = cursor.getBoundingClientRect();
console.log(circle);

window.addEventListener("mousemove", (e) => {
    gsap.to(cursor,{duration: 0.3,
            left: e.pageX - circle.width/2, 
            top: e.pageY- circle.height/2,
        });
});
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€