ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๊ฒฐ๊ณผ๋ฌผ

 

์‚ฌ์šฉ์ž๊ฐ€ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ, colors ๋ฐฐ์—ด์—์„œ ๋‘ ๊ฐœ์˜ ์ƒ‰์ƒ์ด ๋žœ๋ค ์„ ํƒ๋˜๊ณ  ๊ทธ๋ผ๋ฐ์ด์…˜์œผ๋กœ ๋ฐ”๋€Œ๋Š” ์ด๋ฒคํŠธ๋ฅผ ์‹คํ–‰ํ•ด๋ณด์ž.

 


ํ•„์š”ํ•œ ๊ฒƒ

  • linear-gradient : ์„ ํ˜• ๊ทธ๋Ÿฌ๋ฐ์ด์…˜์„ ๋งŒ๋“ค์–ด์ฃผ๋Š” CSS ํ•จ์ˆ˜ 
linear-gradient(๋ฐฉํ–ฅ, ์ƒ‰1, ์ƒ‰2,...)

 

background: linear-gradient(orange, yellow);

๋ฐฉํ–ฅ์˜ ๊ธฐ๋ณธ ๊ฐ’์€ ์œ„->์•„๋ž˜ ์ด๊ณ  ๋ฐฉํ–ฅ์€ ๊ฐ๋„๋กœ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

background: linear-gradient(90deg, pink, orange);

 

  • Math.floor() : ์ฃผ์–ด์ง„ ์ˆซ์ž์˜ ์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ๋‚ด๋ฆผํ•ด์„œ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
  • Math.random() : 0์—์„œ 1๋ณด๋‹ค ์ž‘์€ ๋ฒ”์œ„์˜ ๋‚œ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ 

 

 

  • length : ๋ฐฐ์—ด์˜ ๊ธธ์ด(์š”์†Œ์˜ ๊ฐœ์ˆ˜)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ”„๋กœํผํ‹ฐ 

 

 

๊ณผ์ •

const button = document.querySelector("button");

function handleButton() {
  ...
}
button.addEventListener("click", handleButton);

 

1. ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ๋ฐฐ๊ฒฝ ์ƒ‰์ƒ์ด ๋žœ๋ค์œผ๋กœ ๋ณ€๊ฒฝ๋˜์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ๊ฑธ์–ด์ฃผ๊ณ  ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

function handleButton() {
  const color1 = colors[Math.floor(Math.random() * colors.length)];
  const color2 = colors[Math.floor(Math.random() * colors.length)];
  }
}

 

2. ๋ฐฐ๊ฒฝ์˜ ์ƒ‰์ƒ์€ colors ๋ผ๋Š” ๋ฐฐ์—ด์—์„œ ๋ฐ›์•„์™€์•ผ ํ•œ๋‹ค.

์ด๋•Œ, ๋žœ๋ค์œผ๋กœ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋„๋ก Math.floor() ์™€ Math.random() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ 

colors ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋งŒํผ ๋žœ๋ค ๋‚œ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด length ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ

๋‘ ๊ฐœ์˜ ์ปฌ๋Ÿฌ ๋ณ€์ˆ˜ color1 / color2 ๋ฅผ ๋งŒ๋“ ๋‹ค. 

 

function handleButton() {
  const color1 = colors[Math.floor(Math.random() * colors.length)];
  const color2 = colors[Math.floor(Math.random() * colors.length)];
  if (color1 !== color2) {
    document.body.style.background = `linear-gradient(90deg,
      ${color1},
      ${color2}
    )`;
  }
}

 

3. color1๊ณผ color2์˜ ์ƒ‰์ƒ์ด ๋˜‘๊ฐ™์œผ๋ฉด ๊ทธ๋ผ๋ฐ์ด์…˜์ด ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ์ƒ‰์ƒ์ด ๊ฐ™์ง€ ์•Š์„ ๋•Œ

body์˜ ์Šคํƒ€์ผ ์ค‘ background ์†์„ฑ์— linear-gradient() ํ•จ์ˆ˜๋กœ ๊ทธ๋ผ๋ฐ์ด์…˜์„ ์ ์šฉํ•œ๋‹ค.

 

js์—์„œ css์Šคํƒ€์ผ์„ ์ ์šฉํ•  ๋•Œ์—๋Š” style.[์†์„ฑ] = "์†์„ฑ๊ฐ’"; ์˜ ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•˜๋ฏ€๋กœ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด [ ``(๋ฐฑํ‹ฑ) ๊ณผ ${} ]์„ ์‚ฌ์šฉํ•œ๋‹ค. 

 

 

 

์ „์ฒด ์ฝ”๋“œ

const colors = [
  "#ef5777",
  "#575fcf",
  "#4bcffa",
  "#34e7e4",
  "#0be881",
  "#f53b57",
  "#3c40c6",
  "#0fbcf9",
  "#00d8d6",
  "#05c46b",
  "#ffc048",
  "#ffdd59",
  "#ff5e57",
  "#d2dae2",
  "#485460",
  "#ffa801",
  "#ffd32a",
  "#ff3f34"
];

const button = document.querySelector("button");

function handleButton() {
  const color1 = colors[Math.floor(Math.random() * colors.length)];
  const color2 = colors[Math.floor(Math.random() * colors.length)];
  if (color1 !== color2) {
    document.body.style.background = `linear-gradient(90deg,
      ${color1},
      ${color2}
    )`;
  }
}
button.addEventListener("click", handleButton);

์ฐธ๊ณ 

linear-gradient()

 

linear-gradient() - CSS: Cascading Style Sheets | MDN

The linear-gradient() CSS function creates an image consisting of a progressive transition between two or more colors along a straight line. Its result is an object of the <gradient> data type, which is a special kind of <image>.

developer.mozilla.org

Math.floor()

 

Math.floor() - JavaScript | MDN

The Math.floor() function returns the largest integer less than or equal to a given number.

developer.mozilla.org

Math.random()

 

Math.random() - JavaScript | MDN

The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range. The implementati

developer.mozilla.org

length

 

Array.prototype.length - JavaScript | MDN

The length property of an object which is an instance of type Array sets or returns the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.

developer.mozilla.org

 

๋Œ“๊ธ€