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

[Mouse Effect] ๋งˆ์šฐ์Šค ์ดํŽ™ํŠธ 01

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

๋งˆ์šฐ์Šค ์ดํŽ™ํŠธ 01

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๊ทธ ๋™์•ˆ ๋ฐฐ์› ๋˜ JavaScript๋ฅผ ์ด์šฉํ•˜์—ฌ ๋งˆ์šฐ์Šค ์ดํŽ™ํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


HTML ์†Œ์Šค ๋ณด๊ธฐ

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

<body class="img04"> <!-- ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ด๋ฏธ์ง€ / CSS๋กœ ์ด๋ฏธ์ง€ ์ถœ๋ ฅ์‹œํ‚ด -->
    <header id="header">
        <h1>JAVASCRIPT MOUSE EFFECT 01</h1>
        <p>๋งˆ์šฐ์Šค ์ดํŽ™ํŠธ - ๋งˆ์šฐ์Šค ๋”ฐ๋ผ๋‹ค๋‹ˆ๊ธฐ</p>
        <ul>
            <li class="active"><a href="mouseEffect01.html">01</a></li>
            <li><a href="mouseEffect02.html">02</a></li>
            <li><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>

      <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>
    <!-- //main -->

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

JavaScript ์†Œ์Šค ๋ณด๊ธฐ : ๋งˆ์šฐ์Šค ์ปค์„œ ๋งŒ๋“ค๊ณ  ์ ์šฉํ•˜๊ธฐ

๋จผ์ € ๋งˆ์šฐ์Šค ์›€์ง์ด๋Š” ๊ฐ’์„ ๊ฐ€์ ธ์™€ HTML ์ƒ์— ํ‘œ๊ธฐ์‹œํ‚จ ๋‹ค์Œ, CSS๋กœ ๋งŒ๋“  ๋งˆ์šฐ์Šค ์ปค์„œ๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ const ์ƒ์ˆ˜์— ์ €์žฅ์‹œ์ผœ ์ ์šฉํ•œ๋‹ค.

// ๋งˆ์šฐ์Šค ์›€์ง์ด๋Š” ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ - ๋งˆ์šฐ์Šค ์ปค์„œ ์ขŒํ‘œ๊ฐ’์„ ํ‘œ์‹œ์‹œํ‚ด
// mouseInfo๋กœ๋ถ€ํ„ฐ ๋–จ์–ด์ง„ ๊ฐ’ == ์ขŒํ‘œ๊ฐ’
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;
});

// css๋กœ ๋งŒ๋“  ๋งˆ์šฐ์Šค์ปค์„œ ์ ์šฉ์‹œํ‚ค๊ธฐ
const cursor = document.querySelector(".mouse__cursor");

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

JavaScript ์†Œ์Šค ๋ณด๊ธฐ : ๋งŒ๋“  ๋งˆ์šฐ์Šค ์ปค์„œ์— CSS ์ž…ํžˆ๊ธฐ

๋งˆ์šฐ์Šค ์ปค์„œ๋ฅผ ์˜ฌ๋ฆฌ๋Š” ๊ณณ๋งˆ๋‹ค(= hoverํ•˜๋Š” ๊ณณ๋งˆ๋‹ค) ์ปค์„œ ๋ชจ์–‘์„ ๋‹ค๋ฅด๊ฒŒ CSS์—์„œ ์„ค์ •ํ•ด์ค€ ๋‹ค์Œ, ๊ทธ๊ฑธ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ†ตํ•ด ์ ์šฉ์‹œ์ผœ์ฃผ๋„๋ก ํ•œ๋‹ค. forEach๋ฌธ๊ณผ getAtrribute()๋ฅผ ์“ธ ๊ฒƒ์„ ๊ถŒ์žฅ.

ํ•˜๋‹จ์˜ CSS ์†Œ์Šค ๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ๋งˆ์šฐ์Šค ์ปค์„œ์— ์ž…ํ˜€์ง„ CSS ์†์„ฑ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

// ์ ์šฉํ•œ ๋งˆ์šฐ์Šค์ปค์„œ์— CSS ์ž…ํžˆ๊ธฐ (์ˆ˜๋™)
// 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));
//   });
// });

// getAtrribute() : 'style'์ด ๋‹ด๊น€ ํด๋ž˜์Šค๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์™€ ๋” ์ •ํ™•ํ•œ ๋ฐฉ๋ฒ•
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);
  });
});

๊ฒฐ๊ณผ

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€


Reference Book

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