const inputs = document.querySelectorAll('.controls input');
function handleUpdate() {
const suffix = this.dataset.sizing || ''; // || ์ ๋ฃ๋์ด์ ๋ data-sizing๊ฐ์ด ๊ฐ์๊ฒฝ์ฐ ์๊ธฐ๋๋ฌธ์ ๋ฃ์ด์ค ์์๋๋ ๊ณต๋ฐฑ์ฒ๋ฆฌ๋๊ฒ ํด์ค
document.documentElement.style.setProperty(`--${this.name}`, this.value + suffix);
// css ์์ฑ์ ์ฌํ ๋น ์ํด (IE8 ์ดํ๋ setaAttribute๋ฅผ ์จ์ผํจ)
}
inputs.forEach(input => input.addEventListener('change', handleUpdate));
inputs.forEach(input => input.addEventListener('mousemove', handleUpdate));
<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="textSize" class="opt-title">Font size</label>
<input type="range" name="textSize" min="20" max="78" value="14" 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">
:root {
--base: #ffc600;
--spacing: 10px;
--blur: 10px;
--textSize:20px;
}
img {
padding: var(--spacing);
background: var(--base);
filter: blur(var(--blur));
}
.hl {
color: var(--base);
font-size:var(--textSize);
}
/*
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;
}
:root
:root
๋ ๊ฐ์ ์ ํ์์ด๊ธฐ ๋๋ฌธ์, html ์
๋ ํฐ๋ณด๋ค๋ ์ฐ์ ์์๊ฐ ๋์ต๋๋ค. ๊ทธ๋์ :root์ html์ ๋ํ ์คํ์ผ์ ๋์์ ์ ์ธํ๋ฉด, :root์ ๋ํ ์คํ์ผ์ด ์ฐ์ ๋ฉ๋๋ค.
root๋ ์ ์ญ ์ค์ฝํ์ CSS ๋ณ์(์ฌ์ฉ์ ์ ์ CSS์์ฑ)๋ฅผ ์ ์ธํ ๋ ์ฌ์ฉ