[Javascript / CSS] linear-gradient() μ¬μ©νμ¬ λλ€μΌλ‘ λ°°κ²½ μμ λ°κΎΈκΈ°
μ¬μ©μκ° λ²νΌ ν΄λ¦ μ, 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() - 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() - JavaScript | MDN
The Math.floor() function returns the largest integer less than or equal to a given number.
developer.mozilla.org
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
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