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

๋งˆ์šฐ์Šค ์ดํŽ™ํŠธ 2. ๋งˆ์šฐ์Šค ๋”ฐ๋ผ๋‹ค๋‹ˆ๊ธฐ (GSAP)

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

Javascript Mouse Effect

Mouse ์ดํŽ™ํŠธ ์ค‘ GSAP์„ ์ด์šฉํ•œ ๋งˆ์šฐ์Šค ๋”ฐ๋ผ๋‹ค๋‹ˆ๊ธฐ ํšจ๊ณผ์ž…๋‹ˆ๋‹ค.

HTML

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

<main id="main">
    <section id="mouseType">
        <div class="mouse__cursor"></div>
        <div class="mouse__cursor2"></div>
        
        <div class="mouse__wrap">
            <p>๋ฉ‹์žˆ๋Š” ๋ช…์–ธ<span class="style1">hoc</span> <span class="style2">quoque</span> <span
                    class="style3">transibit</span>๋ฉ‹์žˆ๋Š” ๋ช…์–ธ</p>
            <p>๋ฉ‹์žˆ๋Š” ๋ช…์–ธ<span class="style4">์ด</span> <span class="style5">๋˜ํ•œ</span> <span
                    class="style6">์ง€๋‚˜๊ฐ€๋ฆฌ๋ผ.</span>๋ฉ‹์žˆ๋Š” ๋ช…์–ธ</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: 10px;
    height: 10px;
    z-index: 10002;
    border-radius: 50%;
    background: rgba(255,255,255,0.1);
    user-select: none;
    pointer-events: none;
    transition: transform 0.3s, opacity 0.2s;
}

.mouse__cursor2 {
    position: absolute;
    left: 0;
    top: 0;
    width: 30px;
    height: 30px;
    z-index: 10001;
    border-radius: 50%;
    background: rgba(255,255,255,0.3);
    user-select: none;
    pointer-events: none;
    transition: transform 0.3s, opacity 0.2s;

}

.mouse__cursor.active {
    transform: scale(0);
}
.mouse__cursor2.active {
    transform: scale(5);
    background: rgba(255,166,0,0.6);
}

.mouse__cursor.active2 {
    transform: scale(0);
}
.mouse__cursor2.active2 {
    transform: scale(3);
    background: rgba(255, 161, 161, 0.541);
}

.mouse__cursor.active3 {
    transform: scale(0);
}
.mouse__cursor2.active3 {
    transform: scale(3);
    background: rgba(142, 223, 255, 0.6);
}

Javascript

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

const cursor = document.querySelector(".mouse__cursor");
const cursor2 = document.querySelector(".mouse__cursor2");
const span = document.querySelectorAll(".mouse__wrap span");
const span2 = document.querySelectorAll("#header ul li");
const span3 = document.querySelectorAll(".modal__wrap .modal__btn");

window.addEventListener("mousemove" , e => {
    // ์ปค์„œ ์ขŒํ‘œ๊ฐ’ ํ• ๋‹น
    // cursor.style.left = e.pageX -5 + "px";
    // cursor.style.top = e.pageY -5 + "px";
    // cursor2.style.left = e.pageX -15 + "px";
    // cursor2.style.top = e.pageY -15 + "px";

    // GSAP
    gsap.to(cursor, {duration: 0.3, left: e.pageX -5, top: e.pageY -5})
    gsap.to(cursor2, {duration: 0.8, left: e.pageX -15, top: e.pageY -15})


    // ์˜ค๋ฒ„ ํšจ๊ณผ  
    // mouseenter / mouseover / ์ด๋ฒคํŠธ ๋ฒ„๋ธ”๋ง
    // (ํ™”์‚ดํ‘œํ•จ์ˆ˜๋ฅผ ์“ฐ๋ฉด this ๋ฅผ ์‚ฌ์šฉํ• ์ˆ˜ ์—†๋‹ค!!)
    span.forEach(span => {
        span.addEventListener("mouseenter", ()=>{
            cursor.classList.add("active");
            cursor2.classList.add("active");
        });
        span.addEventListener("mouseleave", ()=>{
            cursor.classList.remove("active");
            cursor2.classList.remove("active");
        });
    });

    span2.forEach(span2 => {
        span2.addEventListener("mouseenter", ()=>{
            cursor.classList.add("active2");
            cursor2.classList.add("active2");
        });
        span2.addEventListener("mouseleave", ()=>{
            cursor.classList.remove("active2");
            cursor2.classList.remove("active2");
        });
    });

    span3.forEach(span3 => {
        span3.addEventListener("mouseenter", ()=>{
            cursor.classList.add("active3");
            cursor2.classList.add("active3");
        });
        span3.addEventListener("mouseleave", ()=>{
            cursor.classList.remove("active3");
            cursor2.classList.remove("active3");
        });
    });
});
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€