[JavaScript30] ๐Ÿ–ผ 03. CSS Variable

์กฐ์ค€ํ˜•ยท2021๋…„ 7์›” 4์ผ
1

JavaScript30

๋ชฉ๋ก ๋ณด๊ธฐ
3/30

๐Ÿ–ผ 03. CSS Variable

spacing๊ณผ blur, base color๋ฅผ ์กฐ์ ˆํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ๋ณ€ํ™”์‹œํ‚ค๊ธฐ.

์ดˆ๊ธฐ์ฝ”๋“œ

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Scoped CSS Variables and JS</title>
</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>

    /*
      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;
    }
  </style>

  <script>
  </script>

</body>
</html>

์ดˆ๊ธฐํ™”๋ฉด

๐ŸŒ ์ƒˆ๋กœ ์•Œ๊ฒŒ ๋œ ๊ฒƒ

๐Ÿ‘‰ data-name

data-sizing์œผ๋กœ sizing์ด๋ž€ ๋ณ€์ˆ˜์— px์„ ์ €์žฅํ•จ.

์ฐธ๊ณ  : https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/data-*

๐Ÿ‘‰ :root

๋ฌธ์„œ ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ์š”์†Œ๋ฅผ ์„ ํƒํ•œ๋‹ค.

์ฐธ๊ณ  : https://developer.mozilla.org/en-US/docs/Web/CSS/:root

๐Ÿ‘‰ input type="range"

์–ด๋Š์ •๋„ ๋ฒ”์œ„์˜ range bar๊ฐ€ ์ƒ๊ธด๋‹ค.

์ฐธ๊ณ  : https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/range

๐ŸŒ ๊ณผ์ •

๐Ÿ‘‰ 1. CSS๋ณ€์ˆ˜ ๋งŒ๋“ค๊ธฐ.

base์ƒ‰์ƒ, ๊ธฐ๋ณธ ์—ฌ๋ฐฑ, blur์ •๋„.

:root{
	--base: #ffc600;
	--spacing: 10px;
	--blur: 10px;
}

๐Ÿ‘‰ 2. ๋ณ€์ˆ˜ ์ง€์ •

img์™€ class "h1"์— ๋ณ€์ˆ˜์‚ฌ์šฉ.

img{
    padding: var(--spacing);
    background: var(--base);
    filter: blur(var(--blur));
}
.h1{
    color: var(--base);
}

๐Ÿ‘‰ 3. javaScript๋กœ ๊ฐ’์ด ๋ณ€ํ•  ๋•Œ ๋ณ€์ˆ˜ ๋™์ž‘ํ•˜๊ฒŒ ์ž‘์„ฑ

<script>
    // .controls์˜ ๋ชจ๋“  input ์„ ํƒ.
    const inputs = document.querySelectorAll('.controls input')
    function handleUpdate(){
        // console.log(this.value);
        // console.log(this.dataset);
        const suffix = this.dataset.sizing || '';
        // console.log(suffix);

        //update variable
        document.documentElement.style.setProperty(`--${this.name}`, this.value + suffix);
    }
    // use arrow function
    inputs.forEach(input => input.addEventListener('change', handleUpdate));
    inputs.forEach(input => input.addEventListener('mousemove', handleUpdate));
</script>
const inputs = document.querySelectorAll('.controls input')

.controls์— ์žˆ๋Š” ๋ชจ๋“  inputํƒœ๊ทธ๋ฅผ ์„ ํƒํ•จ.

inputs.forEach(input => input.addEventListener('change', handleUpdate));
inputs.forEach(input => input.addEventListener('mousemove', handleUpdate));

๊ฐ’์ด ๋ณ€ํ•˜๊ฑฐ๋‚˜, ๋งˆ์šฐ์Šค๊ฐ€ ์›€์ง์ผ ๋•Œ handleUpdate๋ฉ”์„œ๋“œ๊ฐ€ ๋™์ž‘.

 function handleUpdate(){
        const suffix = this.dataset.sizing || '';

        //update variable
        document.documentElement.style.setProperty(`--${this.name}`, this.value + suffix);
    }

this.dataset.sizing์€ <input id="blur" type="range" name="blur" min="0" max="25" value="10" data-sizing="px">์—์„œ data-sizing="px"๋กœ ์ง€์ •ํ•ด ๋†“์Œ.

์ƒ‰์„ ์ •ํ•˜๋Š” ๊ณณ์—์„œ๋Š” data-sizing์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ''๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ๋œ๋‹ค.

๊ฐ’์„ ๋ณ€๊ฒฝํ•จ. ๋ณ€๊ฒฝ๋˜๋Š” ๊ฐ’์€ '๋ฐ”๋€ ๊ฐ’ + suffix'๋กœ ๋ณ€๊ฒฝ๋จ.

profile
๊นƒํ—ˆ๋ธŒ : github.com/JuneHyung

0๊ฐœ์˜ ๋Œ“๊ธ€