Front-end πŸ”₯πŸ–€/JavaScript

[Javascript / CSS] linear-gradient() μ‚¬μš©ν•˜μ—¬ 랜덀으둜 λ°°κ²½ 색상 λ°”κΎΈκΈ°

stable_sound 2022. 5. 19. 16:12

κ²°κ³Όλ¬Ό

 

μ‚¬μš©μžκ°€ λ²„νŠΌ 클릭 μ‹œ, 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