๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Javascript Effect

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋งˆ์šฐ์Šค ํšจ๊ณผ : ๋งˆ์šฐ์Šค ๋”ฐ๋ผ๋‹ค๋‹ˆ๊ธฐ

by ์ฝ”ํŒŒ์นด 2022. 9. 6.
728x90

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ : ๋งˆ์šฐ์Šค ํšจ๊ณผ : ๋งˆ์šฐ์Šค ๋”ฐ๋ผ๋‹ค๋‹ˆ๊ธฐ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋‹ค์–‘ํ•œ ํšจ๊ณผ๋“ค ์ค‘, ๋งˆ์šฐ์Šค ํšจ๊ณผ์ž…๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋งˆ์šฐ์Šค์˜ ์œ„์น˜๊ฐ’์„ ์ฐพ๊ณ , ์œ„์น˜๊ฐ’์— ๋”ฐ๋ผ ์ปค์„œ๊ฐ€ ์ด๋™ํ•˜๊ณ  ๋ณ€ํ™”ํ•˜๊ฒŒ๋” ์ž‘์—…ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


HTML ์†Œ์Šค

HTML๋กœ๋Š” ๋จผ์ € ๋งˆ์šฐ์Šค์˜ ์ปค์„œ๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๊ณ , ๋ช…์–ธ์ด ๋“ค์–ด๊ฐˆ mouse__wrap ๋ฐ•์Šค, ๋งˆ์šฐ์Šค์˜ ์œ„์น˜๊ฐ’์„ ์ž‘์„ฑํ•  mouse__info ๋ฐ•์Šค๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

<section id="mouseType01">
    <div class="mouse__cursor"></div>
    <div class="mouse__wrap">
        <p>The <span class="style1">future</span> depends on <span class="style2">what</span> we do in the <span class="style3">present</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>

CSS ์†Œ์Šค

CSS๋กœ ์Šคํƒ€์ผ ์†์„ฑ์„ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๋ช…์–ธ์—์„œ ๋ฐ‘์ค„์ด ๊ทธ์ธ ๋‹จ์–ด์— ๊ฐ๊ฐ ๋งˆ์šฐ์Šค๋ฅผ ์˜ค๋ฒ„ํ•˜๋ฉด ๋‹ค๋ฅธ ์Šคํƒ€์ผ๋กœ ๋ณ€ํ™”ํ•˜๋„๋ก ํŠธ๋žœ์Šคํผ ํšจ๊ณผ๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

/* mouseType */
.mouse__wrap {
    width: 100%;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    overflow: hidden;
    flex-direction: column;
    cursor: none;
}
.mouse__wrap p {
    font-size: 2vw;
    line-height: 2;
    font-weight: 300;
}
.mouse__wrap p:last-child {
    font-size: 3vw;
}
.mouse__wrap p span {
    border-bottom: 0.35vw dashed rgb(77, 46, 128);
    color: rgb(77, 46, 128);
}
@media (max-width: 800px) {
    .mouse__wrap p {
        font-size: 20px;
        padding: 0 20px;
        text-align: center;
        line-height: 1.5;
        word-break: keep-all;
        margin-bottom: 10px;
    }
    .mouse__wrap p:last-child {
        font-size: 40px;
    }
}
.mouse__cursor {
    z-index: 2001;
    position: absolute;
    left: 0;
    top: 0;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    border: 3px solid #fff;
    background-color: rgba(255, 255, 255, 0.1);
    /* ๋งˆ์šฐ์Šค ์ปค์„œ ์„ ํƒ, ํด๋ฆญ X */
    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, 0, 0, 0.5);
    border-color: red;
}
.mouse__cursor.style2 {
    background-color: rgba(255, 166, 0, 0.5);
    border-color: orange;
    transform: scale(2) rotateY(720deg);
}
.mouse__cursor.style3 {
    background-color: rgba(255, 255, 0, 0.5);
    border-color: yellow;
    transform: scale(1.5) rotateX(720deg);
}
.mouse__cursor.style4 {
    background-color: rgba(172, 255, 47, 0.5);
    border-color: greenyellow;
    transform: scale(10);
}
.mouse__cursor.style5 {
    background-color: rgba(0, 0, 255, 0.5);
    border-color: blue;
    transform: scale(0.1);
}
.mouse__cursor.style6 {
    background-color: rgba(137, 43, 226, 0.5);
    border-color: blueviolet;
    border-radius: 0;
    transform: rotate(720deg);
}
.mouse__info {
    z-index: 2001;
    position: absolute;
    left: 20px;
    bottom: 10px;
    font-size: 14px;
    line-height: 1.6;
    color: #fff;
}

JAVASCRIPT ์†Œ์Šค

๋Œ€๋ง์˜ JAVASCIPRT ์†Œ์Šค์ž…๋‹ˆ๋‹ค. ์ปค์„œ์˜ ์œ„์น˜๊ฐ’์„ ์ผ์ผ์ด ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, for๋ฌธ๊ณผ forEach๋ฌธ์œผ๋กœ๋„ ์ž‘์„ฑํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

window.addEventListener("mousemove", (event) => {
    document.querySelector(".clientX").innerHTML = event.clientX;
    document.querySelector(".clientY").innerHTML = event.clientY;
    document.querySelector(".offsetX").innerHTML = event.offsetX;
    document.querySelector(".offsetY").innerHTML = event.offsetY;
    document.querySelector(".pageX").innerHTML = event.pageX;
    document.querySelector(".pageY").innerHTML = event.pageY;
    document.querySelector(".screenX").innerHTML = event.screenX;
    document.querySelector(".screenY").innerHTML = 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.querySelectorAll(".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

๋Œ“๊ธ€

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๐Ÿฆ™

CSS
๊ด‘๊ณ  ์ค€๋น„์ค‘