JS) Window, resize event

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

JavaScript

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

https://developer.mozilla.org/en-US/docs/Web/API/Window




Window, resize event๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์ž‘์„ฑํ–ˆ๋˜ ์ฝ”๋“œ๋ฅผ ๋ธ”๋กœ๊ทธ์—๋„ ์ €์žฅ์šฉ์œผ๋กœ ์˜ฌ๋ ค๋ณด์ž!



1. ๋ธŒ๋ผ์šฐ์ € width size์— ๋”ฐ๋ผ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒ‰์ƒ์ด ๋ฐ”๋€Œ๋Š” ์ฝ”๋“œ.

function changeColor() {
  const width = window.innerWidth;
  console.log(width);
  if (width <= 750) {
    document.body.style.background = "yellow";
  } else if (width > 750 && width <= 1300) {
    document.body.style.background = "purple";
  } else {
    document.body.style.background = "white";
  }
}

window.addEventListener("resize", changeColor);

๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒ‰์ƒ ๋ฐ”๊พผ๋‹ค๊ณ  ๊ฒ€์ƒ‰์„ ๊ฝค๋‚˜ ํ–ˆ์—ˆ๋‹ค..๐Ÿ˜…





2. ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒ‰์ƒ์ด ๋ฐ”๋€Œ๋Š” ์ฝ”๋“œ

<!--HTML-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button id="yellowBtn">yellow</button>
    <button id="purpleBtn">purple</button>
    <script src="index.js"></script>
  </body>
</html>

//Javascript
const yellowBtn = document.querySelector("#yellowBtn");
const purpleBtn = document.querySelector("#purpleBtn");

function ChangeYellowColor() {
  document.body.style.backgroundColor = "yellow";
}

function ChangePurpleColor() {
  document.body.style.backgroundColor = "purple";
}

yellowBtn.addEventListener("click", ChangeYellowColor);
purpleBtn.addEventListener("click", ChangePurpleColor);



3. ๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜ ์ฝ”๋“œ๋Š” classList์˜ add๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด๋ณธ ์ฝ”๋“œ.

<!--HTML-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style.css" />
    <title>Document</title>
  </head>
  <body>
    <div>
      <div id="greet">Hello!</div>
    </div>
    <script src="index.js"></script>
  </body>
</html>

/*CSS*/
body {
  margin: 0;
}
div {
  width: 100vw;
  height: 100vh;
}
#greet {
  font-size: 50px;
  font-weight: bold;
  color: white;
}

//Javascript
const colors = ["blue", "orange", "purple", "white"];
const div = document.querySelector("div");
div.style.backgroundColor = colors[0];

function changeColor() {
  const width = window.innerWidth;
  if (width <= 750) {
    div.classList.add("blueColor");
    div.style.backgroundColor = colors[0];
  } else if (width > 750 && width <= 1300) {
    div.classList.add("orangeColor");
    div.style.backgroundColor = colors[1];
  } else {
    div.classList.add("purpleColor");
    div.style.backgroundColor = colors[2];
  }
}
window.addEventListener("resize", changeColor);

nice~ =D

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

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