๐ ์๊ตฌ์ฌํญ
- spacing, blur, base color์ ๋ํ input์ ๋ง์ฐ์ค๋ก ์กฐ์ ํ๋ฉด, ํด๋นํ๋ ๊ฐ์ผ๋ก CSS ์์๊ฐ ์ ๋ฐ์ดํธ ๋๋ค.
- css variable์ ์ฌ์ฉํ๋ค.
- ์์ ์ด๋ฒคํธ๋ ๋ง์ฐ์ค๊ฐ ์์ง์์ ๋ฐ๋ผ ์ค์๊ฐ์ผ๋ก ๋ฐ์๋์ด์ผ ํ๋ค.
โจ ๊ฒฐ๊ณผํ๋ฉด
๐ป ์์ค์ฝ๋
๐ ๋ด๊ฐ ์์ฑํ ์ฝ๋
HTML, CSS
<!DOCTYPE html>
<html lang="en">
<head>
...
<script src="variables.js" defer></script>
</head>
<body>
<h2>Update CSS Variables with <span class="hl">JS</span></h2>
...
<style>
:root {
/* color, space, blur์ ๊ธฐ๋ณธ๊ฐ */
--color-base: #ffc600;
--space-base: 10px;
--blur-base: 10px;
}
img {
transform: translate(var(--space-base));
filter: blur(var(--blur-base));
}
.hl {
color: var(--color-base);
}
</style>
</body>
</html>
css variable์ ์ฌ์ฉ์ ์ํด :root์ ๊ฐ๊ฐ base variable์ ์ ์ธํด ์ฃผ์๊ณ , ํ์ํ ์์์ ํด๋น css variable์ ์ฌ์ฉํ์ฌ ๊ฐ์ ๋์ ํด ์ฃผ์๋ค.
JS
"use strict";
// root์ ์ ์ธ๋ css variable์ ์ ์ดํ๊ธฐ ์ํด ์ ์ธ
let root = document.querySelector(":root");
// ๊ฐ input์ addEventListener๋ก ์ด๋ฒคํธ๋ฅผ ์ถ๊ฐํ๊ธฐ ์ํด ์ ์ธ
const color = document.getElementById("base");
const blur = document.getElementById("blur");
const spacing = document.getElementById("spacing");
// 1. blur ์ด๋ฒคํธ
blur.addEventListener("mousemove", (e) => {
root.style.setProperty("--blur-base", `${e.target.value}px`);
});
// 2. spacing ์ด๋ฒคํธ
spacing.addEventListener("mousemove", (e) => {
root.style.setProperty(
"--space-base",
`${e.target.value}px, ${e.target.value}px`
);
});
// 3. color ์ด๋ฒคํธ
color.addEventListener("mousemove", (e) => {
root.style.setProperty("--color-base", `${e.target.value}`);
});
ํ์ํ input ์์๋ค์ getElementById๋ฅผ ์ด์ฉํด์ ๊ฐ์ ธ์จ ํ, ๊ฐ๊ฐ addEventListener๋ฅผ ํตํด mousemove ์ด๋ฒคํธ๋ฅผ ์ถ๊ฐํ๋ค. ๊ทธ๋ฆฌ๊ณ input์ ๊ฐ์ด ๋ณํจ์ ๋ฐ๋ผ, setProperty๋ฅผ ํตํด ํด๋น css variable์ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
์ด๋ ๊ฒ ์์ฑํ์ ๋์ ๋ฌธ์ ์ ์, addEventListener๊ฐ ์ค๋ณต ์ฌ์ฉ๋์ด ์ฝ๋๊ฐ ๋ถํ์ํ๊ฒ ๊ธธ์ด์ง๋ค. ๊ฐ์๋ฅผ ํตํด ๊ฐ์ ํ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
๐ค ๊ฐ์ ๋ ๊ฐ์ ์ฝ๋
HTML, CSS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Scoped CSS Variables and JS</title>
<script src="variables_after.js" defer></script>
</head>
<body>
<h2>Update CSS Variables with <span class="hl">JS</span></h2>
<div class="controls">
<label for="spacing">Spacing:</label>
<input
id="spacing"
type="range"
name="spacing"
min="10"
max="200"
value="10"
data-sizing="px"
/>
<label for="blur">Blur:</label>
<input
id="blur"
type="range"
name="blur"
min="0"
max="25"
value="10"
data-sizing="px"
/>
<label for="base">Base Color</label>
<input id="base" type="color" name="base" value="#ffc600" />
</div>
<img src="https://source.unsplash.com/7bwQXzbF6KE/800x500" />
<style>
:root {
/* color, space, blur์ ๊ธฐ๋ณธ๊ฐ */
--base: #ffc600;
--spacing: 10px;
--blur: 10px;
}
/*
misc styles, nothing to do with CSS variables
*/
body {
text-align: center;
background: #193549;
color: white;
font-family: "helvetica neue", sans-serif;
font-weight: 100;
font-size: 50px;
}
.controls {
margin-bottom: 50px;
}
input {
width: 100px;
}
img {
padding: var(--spacing);
background: var(--base);
transform: translate(var(--spacing));
filter: blur(var(--blur));
}
.hl {
color: var(--base);
}
</style>
<script></script>
</body>
</html>
๊ฐ input์๋ name์ด ์ ์ธ๋์ด ์๋๋ฐ, JS์์ ์ ์ดํ๊ธฐ ์ํด css variable์ ์ด๋ฆ์ ๊ฐ input์ name์ผ๋ก ์ ์ธํด ์ฃผ์๋ค. ๋, ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์์ ์ฃผ๊ธฐ ์ํด padding์ ์ฃผ๊ณ , ๊ฐ์ :root์ ์ ์ธํ variable๋ก ์ฃผ์๋ค. ๋ฐ๋ผ์ padding๊ฐ ๋ํ JS์์ ์ ์ด๋๋ค.
JS
"use strict";
// controls div์ ๋ชจ๋ input ์์๋ฅผ ๊ฐ์ ธ์์ ์ด๋ค ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด css๋ฅผ ๋ณ๊ฒฝํ๋ค.
const inputs = document.querySelectorAll(".controls input");
function handleUpdate() {
const suffix = this.dataset.sizing || "";
// px, or nothing์ type=color input์ ๊ฐ๋ฆฌํจ๋ค.
// || ''์ ๋ช
์ํ์ง ์์ผ๋ฉด undefined ์ฒ๋ฆฌ๋๋ค.
document.documentElement.style.setProperty(`--${this.name}`, this.value + suffix);
}
inputs.forEach((input) => input.addEventListener("change", handleUpdate));
inputs.forEach((input) => input.addEventListener("mousemove", handleUpdate));
JS์ฝ๋๋ ํจ์ฌ ์ค์ด๋ค์๋ค. ๊ฐ ์์๋ฅผ ์ผ์ผํ ๊ฐ์ ธ์์ ์ ์ธํ ๋์ ๋ฐฉ์๊ณผ๋ ๋ฌ๋ฆฌ, input ์์๋ฅผ ํ๊บผ๋ฒ์ ๊ฐ์ ธ์์ ํด๋น ์์๋ค์ ๋ณ๊ฒฝ์ด ๋ฐ์ํ๋ฉด ๊ฐ์ ์ ์ฉํด์ฃผ๋ ์์ผ๋ก ์์ฑํ์๋ค.
suffix ๋ณ์๊ฐ์ ๊ฒฝ์ฐ์๋, HTML์์ ์ ์๋ data-sizing์ ๊ฐ์ ธ์ค๋๋ฐ, input type="color"์๋ data-*๊ฐ ์์ผ๋ฏ๋ก dataset์ ๊ฐ์ ธ์ค๋ ๋ถ๋ถ ๋ค์ or nothing(|| '')์ ์์ฑํด ์ฃผ์ด undefined๊ฐ ๋จ๋ ํ์์ ๋ฐฉ์งํ์๋ค.
๐ TIL
css variable
CSS ๋ณ์๋ ๋ฌธ์ ์ ๋ฐ์ ์ผ๋ก ์ฌ์ฌ์ฉํ ์์์ ๊ฐ์ ๋ด์ ์ ์๋๋ก ํ๋ค. ์ด๋์๋ ์ฌ์ฉํ ์ ์๊ฒ ํ๋ ค๋ฉด :root์ ์ ์ํ๋ฉด ๋๋ค. ํน์ element ๋ด์์๋ง ์ฌ์ฉํ๋๋ก ๋ฒ์๋ฅผ ์ง์ ํด์ ์ฌ์ฉํ ์๋ ์๋ค. CSS ๋ณ์๋ CSS ๋ณ์๋ฅผ ์ ์ํ ๋์๋ ๋ณ์ ์์ --๋ฅผ ๋ถ์ฌ์ฃผ๋ฉฐ, ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถํ๋ค. ๊ทธ๋ฆฌ๊ณ ํด๋น ๋ณ์์ ์ ๊ทผํ๋ ค๋ฉด var() ํจ์๋ฅผ ์ฌ์ฉํ๋ค.
<!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" />
<title>Document</title>
<style>
:root {
--color-one: #ff8080;
--color-two: #80dfff;
}
.one {
color: var(--color-one);
}
.two {
color: var(--color-two);
}
</style>
</head>
<body>
<h1 class="one">Hello!</h1>
<h1 class="two">Bye!</h1>
</body>
</html>
์ด ๋, CSS ๋ณ์๋ฅผ :root ํ์์ ํน์ ์์์ ์ ์ธํ๊ฒ ๋๋ฉด, ํด๋น ์์์ ์ ์ธ๋ ์์ ๋ฒ์์ CSS ๋ณ์๊ฐ ๋จผ์ ์ ์ฉ๋๋ค.
<!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" />
<title>Document</title>
<style>
:root {
--color-one: #ff8080;
--color-two: #80dfff;
}
.one {
--color-one: #ffdf80;
color: var(--color-one);
}
.two {
color: var(--color-two);
}
</style>
</head>
<body>
<h1 class="one">Hello!</h1>
<h1 class="two">Bye!</h1>
</body>
</html>
CSS ๋ณ์๋ ์์ ๋์์ด๋ฏ๋ก, ํน์ ์์์ ์ฌ์ฉ์ ์ง์ ๊ฐ์ ์ค์ ํ์ง ์์ ๊ฒฝ์ฐ์๋ ๊ทธ ๋ถ๋ชจ์ ๊ฐ์ ์ฌ์ฉํ๊ฒ ๋๋ค. ๊ทธ๋์ CSS ๋ณ์๋ฅผ ๊ณ๋จ์ ๋ณ์๋ผ๊ณ ๋ ํ๋ค.
<!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" />
<title>Document</title>
<style>
:root {
--color-one: #ff8080;
--color-two: #80dfff;
}
.one {
--color-one: #ffdf80;
color: var(--color-one);
}
.two {
color: var(--color-two);
}
</style>
</head>
<body>
<div class="one">
<h1>one</h1>
<div class="two">
<h1>two</h1>
<div class="three"><h1>three</h1></div>
<div class="four"><h1>four</h1></div>
</div>
</div>
</body>
</html>
setProperty()
์ ์ธ๋ CSS ๋ณ์๋ JS์ getPropertyValue(), setProperty() ๋ฑ์ผ๋ก ์ ์ดํ ์ ์๋ค.
var value = element.style.getPropertyValue(property);
getPropertyValue()๋ ์ธ์๋ก ์ง์ ๋ CSS ์์ฑ์ ๊ฐ์ ๋ฐํํ๋ค.
style.setProperty(propertyName, value, priority);
setProperty()๋ ์ธ์๋ก ์ง์ ๋ CSS ์์ฑ์ ๊ฐ์ ์ค์ ํ๋ค. priority(์ ํ)๋ !important์ ์ฌ์ฉ ์ฌ๋ถ์ด๋ค.
์ ๋๊ฐ์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ --color-two์ ๊ฐ์ ๊ฐ์ ธ์ค๊ณ ๋ณ๊ฒฝํด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
<!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" />
<title>Document</title>
<style>
:root {
--color-one: #ff8080;
--color-two: #80dfff;
}
.one {
--color-one: #ffdf80;
color: var(--color-one);
}
.two {
color: var(--color-two);
}
body {
width: 100%;
display: flex;
justify-content: center;
}
</style>
</head>
<body>
<div class="one">
<h1>one</h1>
<div class="two">
<h1>two</h1>
<div class="three"><h1>three</h1></div>
<div class="four"><h1>four</h1></div>
</div>
</div>
</body>
<script>
document.documentElement.style.setProperty("--color-two", `#80ff80`);
console.log(document.documentElement.style.getPropertyValue("--color-two"));
</script>
</html>
๐ ์ฐธ๊ณ
youtube Wes Bos
https://www.youtube.com/watch?v=AHLNzv13c2I&list=PLu8EoSxDXHP6CGK4YVJhL_VWetA865GOH&index=3
https://developer.mozilla.org/ko/docs/Web/CSS/Using_CSS_custom_properties
https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/setProperty
'language > javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript30] 5. Flexbox + JavaScript Image Gallery (0) | 2021.12.10 |
---|---|
[JavaScript30] 4. Array Cardio Practice (1) (0) | 2021.12.09 |
[JavaScript30] 2. Clock | setInterval(), transform, transition (0) | 2021.12.04 |
[JavaScript30] 1. Drum Kit | data-*, audio, transitionend (0) | 2021.12.04 |
์๋ฐ์คํฌ๋ฆฝํธ ๊ธฐ์ด 4. ํด๋์ค | ํด๋์ค ์์ ์ ์ฝ๋ฐฑ ํจ์ ์ต์ข ์ ๋ฆฌ (0) | 2021.12.02 |