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

๋งˆ์šฐ์Šค ์ดํŽ™ํŠธ - ๋งˆ์šฐ์Šค ๋”ฐ๋ผ๋‹ค๋‹ˆ๊ธฐ

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

Javascript mouse Effect

MouseEffect์ค‘ ๋งˆ์šฐ์Šค ๋”ฐ๋ผ๋‹ค๋‹ˆ๊ธฐ ์ž…๋‹ˆ๋‹ค.

HTML

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

<main id="main">
    <section id="mouseType">
        <div class="mouse__cursor"></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>

    <div class="mouse__info">
        <ul>
            <li>clientX : <span class="clientX">0</span>px</li>
            <li>clientY : <span class="clientY">0</span>px</li>
            <li>offsetX : <span class="offsetX">0</span>px</li>
            <li>offsetY : <span class="offsetY">0</span>px</li>
            <li>pageX : <span class="pageX">0</span>px</li>
            <li>pageY : <span class="pageY">0</span>px</li>
            <li>screenX : <span class="screenX">0</span>px</li>
            <li>screenY : <span class="screenY">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 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 #000;
    color: #000;
}

@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: 50px;
    height: 50px;
    border-radius: 50%;
    border: 3px solid #fff;
    background-color: rgba(0, 0, 0, 0.3);
    user-select: none;
    pointer-events: none;
    transition: 
    background-color 0.3s,
    border-color 0.3s,
    transform 0.6s,
    border-radius 0.3s
    ;
}

.mouse__cursor.style1 {
    background-color: rgba(255, 255, 255, 0.459);
}

.mouse__cursor.style2 {
    background-color: rgba(111, 222, 255, 0.459);
    transform: scale(2) rotateY(720deg);
}

.mouse__cursor.style3 {
    background-color: rgba(255, 255, 255, 0.459);
}

.mouse__cursor.style4 {
    background-color: rgba(255, 255, 255, 0.459);
    transform: scale(10) rotateY(720deg);
}

.mouse__cursor.style5 {
    background-color: rgba(255, 255, 255, 0.459);
}

.mouse__cursor.style6 {
    background-color: rgba(255, 255, 255, 0.459);
    border-radius: 0;
    transform: scale(5) rotateY(720deg);

}

.mouse__info {
    position: absolute;
    left: 20px;
    bottom: 10px;
    font-size: 14px;
    line-height: 1.6;
    color: #fff;
}

Javascript

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

window.addEventListener("mousemove", (event) => {
    document.querySelector(".clientX").innerText = event.clientX;
    document.querySelector(".clientY").innerText = event.clientY;
    document.querySelector(".offsetX").innerText = event.offsetX;
    document.querySelector(".offsetY").innerText = event.offsetY;
    document.querySelector(".pageX").innerText = event.pageX;
    document.querySelector(".pageY").innerText = event.pageY;
    document.querySelector(".screenX").innerText = event.screenX;
    document.querySelector(".screenY").innerText = event.screenY;
});

const cursor = document.querySelector(".mouse__cursor");
window.addEventListener("mousemove", (e) => {
    cursor.style.left = e.clientX - 25 + "px";
    cursor.style.top = e.clientY - 25 + "px";
});

//๊ฐ๊ฐ ํ•˜๋‚˜์”ฉ

// document.querySelector(".style1").addEventListener("mouseover", () => {
//     cursor.classList.add("style1")
// })

// document.querySelector(".style1").addEventListener("mouseout", () => {
//     cursor.classList.remove("style1")
// })

// document.querySelector(".style2").addEventListener("mouseover", () => {
//     cursor.classList.add("style2")
// })

// document.querySelector(".style2").addEventListener("mouseout", () => {
//     cursor.classList.remove("style2")
// })

// document.querySelector(".style3").addEventListener("mouseover", () => {
//     cursor.classList.add("style3")
// })

// document.querySelector(".style3").addEventListener("mouseout", () => {
//     cursor.classList.remove("style3")
// })

// document.querySelector(".style4").addEventListener("mouseover", () => {
//     cursor.classList.add("style4")
// })

// document.querySelector(".style4").addEventListener("mouseout", () => {
//     cursor.classList.remove("style4")
// })

// document.querySelector(".style5").addEventListener("mouseover", () => {
//     cursor.classList.add("style5")
// })

// document.querySelector(".style5").addEventListener("mouseout", () => {
//     cursor.classList.remove("style5")
// })

// document.querySelector(".style6").addEventListener("mouseover", () => {
//     cursor.classList.add("style6")
// })

// document.querySelector(".style6").addEventListener("mouseout", () => {
//     cursor.classList.remove("style6")
// })

//for
for(let i=1; i<=6; i++){
    document.querySelector(".style" + i).addEventListener("mouseover", () => {
    cursor.classList.add("style" + i)
    })

    document.querySelector(".style" + i).addEventListener("mouseout", () => {
    cursor.classList.remove("style" + i)
    })
}

//forEach
// document.querySelector(".mouse_wrap span").forEach((span, num) => {

//     span.addEventListener("mouseover", () => {
//         cursor.classList.add("style" + (num + 1))
//     });
//         span.addEventListener("mouseout", () => {
//         cursor.classList.remove("style" + (num + 1));
//         });
// });

// // getAttribute

document.querySelectorAll(".mouse__wrap span").forEach( span => {
let attr = span.getAttribute("class");
span.addEventListener("mouseover", () => {
    cursor.classList.add(attr)
})
span.addEventListener("mouseout", () => {
    cursor.classList.remove(attr)
})
})
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€