๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Effect/Mouse Effect

[Mouse Effect] ๋งˆ์šฐ์Šค ์ดํŽ™ํŠธ 03 - ์กฐ๋ช… ํšจ๊ณผ

by ์ฝ”๋”ฉ๊ณต์ฑ… 2022. 9. 22.
๋ฐ˜์‘ํ˜•

๋งˆ์šฐ์Šค ์ดํŽ™ํŠธ 03 : ์กฐ๋ช…ํšจ๊ณผ

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” GSAP๋ฅผ ์ด์šฉํ•˜์—ฌ ๋งˆ์šฐ์Šค ์ปค์„œ์— ์กฐ๋ช…ํšจ๊ณผ๋ฅผ ์ž…ํ˜€ ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


HTML ์†Œ์Šค

header, main(๋งˆ์šฐ์Šค ์ปค์„œ & ๋ช…์–ธ), footer(๋ชจ๋‹ฌ ๋ฐ•์Šค) ํฌ๊ฒŒ 3๊ฐ€์ง€๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. footer์˜ ๋ชจ๋‹ฌ ๋ฐ•์Šค ๋‚ด์šฉ์€ ๋„ˆ๋ฌด ๊ธธ์–ด์ง€๋ฏ€๋กœ ์ƒ๋žตํ•œ๋‹ค. ๋ชจ๋‹ฌ ๋ฐ•์Šค๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ๋Š” ์ด์ „์— ์—…๋กœ๋“œํ•œ ์Šฌ๋ผ์ด๋“œ ์ดํŽ™ํŠธ ๊ฒŒ์‹œ๋ฌผ์„ ์ฐธ์กฐํ•  ๊ฒƒ.

<body class="img04">
    <header id="header">
        <h1>JAVASCRIPT MOUSE EFFECT 03</h1>
        <p>๋งˆ์šฐ์Šค ์ดํŽ™ํŠธ - ์กฐ๋ช…ํšจ๊ณผ</p>
        <ul>
            <li><a href="mouseEffect01.html">01</a></li>
            <li><a href="mouseEffect02.html">02</a></li>
            <li class="active"><a href="mouseEffect03.html">03</a></li>
            <li><a href="mouseEffect04.html">04</a></li>
            <li><a href="mouseEffect05.html">05</a></li>
            <li><a href="mouseEffect06.html">06</a></li>
            <li><a href="mouseEffect07.html">07</a></li>
        </ul>
    </header>
    <!-- //header -->

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

        <div class="mouse__wrap">
          <p><span class="style1">The future</span> depends on <span class="style2">what we do</span> in the <span class="style3">present</span>.</p>
          <p><span class="style4">๋ฏธ๋ž˜</span>๋Š” <span class="style5">ํ˜„์žฌ</span> ์šฐ๋ฆฌ๊ฐ€ <span class="style6">๋ฌด์—‡์„ ํ•˜๋Š”๊ฐ€</span>์— ๋‹ฌ๋ ค ์žˆ๋‹ค.</p>
        </div>
      </section>
    </main>
    <!-- //main -->

    <footer>
        <!-- ๋ชจ๋‹ฌ ๋ฐ•์Šค ๋‚ด์šฉ ๋“ค์–ด๊ฐˆ ๊ณณ. ๊ธธ์–ด์ง€๋ฏ€๋กœ ์ƒ๋žต. -->
    </footer>
    <!-- //footer -->
</body>

CSS ์†Œ์Šค

body์— ์‚ฌ์šฉ๋œ ์ด๋ฏธ์ง€์™€ ๋™์ผํ•œ ์ด๋ฏธ์ง€๋ฅผ ๋งˆ์šฐ์Šค ์ปค์„œ ์˜์—ญ์— background-image๋กœ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋งˆ์šฐ์Šค ์ปค์„œ์— ์กฐ๋ช… ํšจ๊ณผ๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋‹ค. ๊ณตํ†ต/์ดˆ๊ธฐํ™” CSS ์†Œ์Šค๋Š” ํ•˜๋‹จ์˜ [CSS ์†Œ์Šค ๋ณด๊ธฐ]๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

.mouse__wrap {
    width: 100%;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    color: white;
    overflow: hidden;
}

.mouse__wrap p {
    font-size: 2vw;
    line-height: 2;
    font-weight: 300;
}

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

.mouse__wrap p span {
    border-bottom: 0.15vw dashed orange;
    color: orange;
}

@media (max-width: 800px) {
    .mouse__wrap p {
        padding: 0 20px;
        font-size: 24px;
        line-height: 1.5;
        text-align: center;
        margin-bottom: 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(../assets/img/effect_bg04.jpg);
    background-size: cover;
    background-position: center;
    background-attachment: fixed;
    border: 10px solid snow;
}

JS ์†Œ์Šค

๋งˆ์šฐ์Šค ์ปค์„œ๊ฐ€ ๋‹ด๊ธด ์ƒ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์›์˜ ๋„ˆ๋น„๊ฐ’์„ ๊ตฌํ•œ ๋‹ค์Œ GSAP ๋ฐฉ์‹์œผ๋กœ ์ปค์„œ์˜ ์›€์ง์ž„์„ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ํ•ด์ฃผ๋„๋ก ํ•œ๋‹ค.

const cursor = document.querySelector(".mouse__cursor");     // ๋งˆ์šฐ์Šค ์ปค์„œ

// ์›์˜ ๋„ˆ๋น„๊ฐ’ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ• 1
//   const circleW = cursor.offsetWidth    // 200
//   const circleH = cursor.offsetWidth;   // 200
//   const circle2 = cursor.clientWidth;   // 190 : ๋ณด๋”๊ฐ’ ์ œ์™ธ

// ์›์˜ ๋„ˆ๋น„๊ฐ’ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ• 2 (๋งŽ์ด ์“ฐ์ด๋Š” ๋ฐฉ๋ฒ• โ˜…โ˜…โ˜…)
const circle = cursor.getBoundingClientRect();
console.log(circle);
//   const DOMRect = {
//     x: 0,
//     y: 0,
//     bottom: 200,
//     height: 200,
//     left: 0,
//     right: 200,
//     top: 0,
//     width: 200
//   }

window.addEventListener("mousemove", (e) => {
  gsap.to(cursor, {
      duration: 0.5,
      left: e.pageX - circle.width/2,
      top: e.pageY - circle.height/2,
  });
});

๊ฒฐ๊ณผ

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€


Reference Book

JavaScript
HTML
CSS
๊ด‘๊ณ  ์ค€๋น„์ค‘์ž…๋‹ˆ๋‹ค.