JS) addEventListener()

Ceciliaยท2022๋…„ 12์›” 29์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
28/36
post-thumbnail

https://developer.mozilla.org/ko/docs/Web/Events




addEventListener์˜ mouse event๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์•˜๋‹ค.
์‚ฌ์šฉํ•ด ๋ณธ ์ด๋ฒคํŠธ ์ค‘ ์ผ๋ถ€ ์ฝ”๋“œ๋ฅผ ์ €์žฅ์šฉ์œผ๋กœ ์˜ฌ๋ฆฐ๋‹ค. :D



์ผ๋‹จ ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋‚ด๊ฐ€ ์ผ๊ณ , ์ฒ˜์Œ์— ๋งŒ๋“ค์—ˆ๋˜ ์ฝ”๋“œ.

const colors = ["#", "#", "#", "#", "#"];
const mouseEvent = document.querySelector("h2");

// mouseenter, mouseleave, contextmenu, resize
function enter() {
  //   console.log(mouseEvent);
  //   console.log(colors[0]);
  mouseEvent.textContent = "Mouse here!";
  mouseEvent.style.color = colors[0];
}

function leave() {
  mouseEvent.textContent = "Mouse gone!";
  mouseEvent.style.color = colors[1];
}

function rightClick() {
  mouseEvent.textContent = "Right click!";
  mouseEvent.style.color = colors[4];
}

function resize() {
  const width = window.innerWidth;
  //   console.log(width);

  if (width > 1000) {
    mouseEvent.style.color = colors[3];
    mouseEvent.textContent = "Resized!";
  }
}

mouseEvent.addEventListener("mouseenter", enter);
mouseEvent.addEventListener("mouseleave", leave);
window.addEventListener("contextmenu", rightClick);
window.addEventListener("resize", resize);



๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ๊ฐ์ฒด(object) ์•ˆ์— ์ „๋ถ€ ํ•ธ๋“ค๋งํ•œ ๋ฒ„์ „.
์ฒ˜์Œ์— ์œ„์˜ ์ฝ”๋“œ ๋Œ€๋กœ ์ž‘์„ฑํ–ˆ๋‹ค๊ฐ€ const ์•ˆ์— ์–ด๋–ป๊ฒŒ ๋„ฃ์ง€..? ์‹ถ์—ˆ๋‹ค. ๐Ÿ˜…
๊ตฌ์กฐ๋Š” ๐Ÿ‘‰์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค!

const EventHandler = {
  enter() {
    mouseEvent.textContent = "Mouse here!";
    mouseEvent.style.color = colors[0];
  },

  leave() {
    mouseEvent.textContent = "Mouse gone!";
    mouseEvent.style.color = colors[1];
  },

  rightClick() {
    mouseEvent.textContent = "Right click!";
    mouseEvent.style.color = colors[2];
  },

  resize() {
    const width = window.innerWidth; //x
    //   console.log(width);

    if (width > 1000) {
      mouseEvent.style.color = colors[3];
      mouseEvent.textContent = "Resized!";
    } else {
      mouseEvent.style.color = colors[4];
      mouseEvent.textContent = "Resized!";
    }
  },
};

mouseEvent.addEventListener("mouseenter", EventHandler.enter);
mouseEvent.addEventListener("mouseleave", EventHandler.leave);
window.addEventListener("contextmenu", EventHandler.rightClick);
window.addEventListener("resize", EventHandler.resize);

EventHandler ์•ˆ์— ํ•จ์ˆ˜๋Š” ์ž˜ ์ž‘์„ฑํ•œ๊ฑฐ๊ฐ™์€๋ฐ, ์™œ ์ž๊พธ ์•ˆ ๋ ๊นŒ.
์ด์œ ๋Š” ๋งจ ์•„๋ž˜ ์ฝ”๋“œ๋“ค์ด์—ˆ๋‹ค.

EventHandler์•ˆ์—์„œ ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜์ด๋‹ˆ,
๋งจ ์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ๊ทธ๋ƒฅ ("mouseenter", enter);๋ผ๊ณ  ํ•˜๋ฉด ๋ชป ์•Œ์•„๋“ฃ๋Š”๋‹ค.
๊ทธ๋ž˜์„œ EventHandler.enter๋ผ๊ณ  ์ž…๋ ฅํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.



๋ง‰์ƒ ๋ฐ”๊ฟ”๋†“๊ณ  ๋‹ค์‹œ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด
์‹คํ–‰๋  ํ•จ์ˆ˜์˜ ๊ฒ‰๋ชจ์Šต๊ณผ EventListener๋ฅผ ์ถ”๊ฐ€ํ• ๋•Œ ์ƒ์„ธ ์œ„์น˜๋งŒ ๋ฐ”๋€Œ์—ˆ๋‹ค.
ํ˜น์‹œ๋‚˜ addEventListener()๋ฅผ ์‚ฌ์šฉํ• ๋•Œ ์ €๋ ‡๊ฒŒ ๊ฐ์ฒด ์•ˆ์— ๋„ฃ๊ฒŒ๋˜๋ฉด ์ด๋ฒˆ ๊ธ€์„ ์ฐธ๊ณ ํ•˜๋ฉด ๋ ๋“ฏํ•˜๋‹ค. ^_~

profile
'์ด๊ฒŒ ์ตœ์„ ์ผ๊นŒ?'๋ผ๋Š” ๊ณ ์ฐฐ์„ ํ†ตํ•ด ๋์—†์ด ์„ฑ์žฅํ•˜๊ณ , ๊ทธ ๊ณผ์ •์„ ์ฆ๊ธฐ๋Š” ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์˜ ์ž…์žฅ์„ ์ƒ๊ฐํ•˜๋ฉฐ ์ตœ์„ ์˜ ํŽธ์˜์„ฑ์„ ์ฐพ๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€