ํฐ์คํ ๋ฆฌ ๋ทฐ
[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() - 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
'Front-end ๐ฅ๐ค > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java Script] ์ ์ผํ ๊ฐ์ ์ ์ฅํ๋ Set ๊ฐ์ฒด๋? (0) | 2022.05.20 |
---|---|
[Java Script] Array.prototype.map() ์ด๋? (0) | 2022.05.19 |
[Java Script] Array.prototype.join() ์ด๋? (0) | 2022.05.18 |
[Java Script] String.prototype.split()์ด๋? (0) | 2022.05.13 |
[Vanilla JS] const ์ let, var ์ ์ฐจ์ด์ (0) | 2022.05.12 |
- Total
- Today
- Yesterday
- indexOf()
- ์กธ์ ์ํ
- ๊ฐค๋ฌ๋ฆฌ๋์ฐ๊ธฐ
- set ๊ฐ์ฒด
- php ๋ฌ๋ ฅ๋ง๋ค๊ธฐ ์์ฉ
- ๋ ธ๋ง๋์ฝ๋
- MySQL
- C์ธ์ด
- ํํ์ด์ง ๋ง๋ค๊ธฐ
- php๊ฒ์ํ๋ง๋ค๊ธฐ
- ์กธ์ ์ํ์ค๋น
- ์ ์ฒ๊ธฐ ์ค๊ธฐ ์ ๋ฆฌ
- ์ ๋ณด์ฒ๋ฆฌ๊ธฐ์ฌ
- ์ด๋๋ฆฌ์
- ํ๋ก๊ทธ๋๋จธ์ค
- ๋ธ๋ก์ฒด์ธ
- php
- JavaScript
- HTML
- ๋ฐฑ์ค
- ํ์ฅ์ค์ต
- ์ ๋ณด์ฒ๋ฆฌ๊ธฐ์ฌ ์ค๊ธฐ ์ ๋ฆฌ
- ์ ๋ณด์ฒ๋ฆฌ๊ธฐ์ฌ ์ค๊ธฐ
- css grid
- ๋ฆฌ์กํธ
- CSS
- ์ ์ฒ๊ธฐ ์ค๊ธฐ
- ์ค๋งํธ์ปจํธ๋ํธ
- DAPP
- ํ์ฅ์ค์ต ๊ธฐ๋ก
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |