JS) use random color(linear-gradient) in JS

Ceciliaยท2023๋…„ 1์›” 5์ผ
0

JavaScript

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

https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/linear-gradient




์˜ค๋Š˜๋„ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ์ €์žฅํ•ด๋ณด์ž.

๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋žœ๋ค ์ƒ‰์ƒ ๋‘ ๊ฐœ๊ฐ€ linear-gradient๋ฅผ ํ†ตํ•ด ๋ฐฐํ•ฉ๋˜์–ด ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒ‰์ƒ์ด ๋ฐ”๋€๋‹ค.

<!--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>
    <button>Click!</button>
    <script src="index.js"></script>
  </body>
</html>


//JavaScript
const btn = document.querySelector("button");
const colors = [
  "์‚ฌ์šฉํ• ",
  "์ƒ‰์ƒ",
  "๋ฐฐ์—ด๋กœ",
  "๋งŒ๋“ค๊ธฐ",
];

// linear-gradient()๋ฅผ ์‚ฌ์šฉํ•  ๋•
// document.body.style.backgroundColor๊ฐ€ ์•„๋‹ˆ๋ผ document.body.style.backgroundImage ์‚ฌ์šฉ
// ๋˜๋Š” ๊ทธ๋ƒฅ document.body.style.background
function changeColor() {
  const randomColor1 = colors[Math.floor(Math.random() * colors.length)];
  const randomColor2 = colors[Math.floor(Math.random() * colors.length)];

  document.body.style.backgroundImage = `linear-gradient(to right, ${randomColor1}, ${randomColor2})`;
  console.log(document.body.style.backgroundImage);
}

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

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