03 - CSS Variables
俗話說的好,一天一蘋果,醫生遠離我
一天一 JS,What the f*ck JavaScript?
small steps every day - 記錄著新手村日記
完成目標
先寫一段 CSS 來改變 img 的 padding & filter & backgorund
畫面有三個變數: Space, Blur, Base Color,當我們在畫面拉動時它會立即更新畫面
index_START.html
html 的整體結構如下所示, class="controls" 包住 label+input 並且在 id="spacing" 中有 type="range" 與 type="range" 可以調整控制項與選擇顏色以更動下方的照片。
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
   | <!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>    </body> </html>
 
  | 
 
Html
Color:使用者可以開啟選擇顏色器選取所要之顏色
  1
   | Color: <input type="color" name="user_color" />
 
  | 
 
Range:使用者可以滑動選擇範圍內的數值
  1
   | <input type="range" name="points" min="1" max="10" />
 
  | 
 
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
   | <style>     :root {       --base: #ffc600;       --spacing: 10px;       --blur: 10px;     }
      img {       padding: var(--spacing);       background: var(--base);       filter: blur(var(--blur));     }
      .hl {       color: var(--base);     }
        </style>
 
  | 
 
JS - step by step
首先,透過 querySelectorAll 抓出 class=”controls” 內的 input 標籤,設定變數為 inputs,記得如這樣抓出來的 inputs 是一個 List,他是沒有辦法使用 map 的!
1 2 3
   | <script> 	const inputs = document.querySelectorAll('.controls input') </script>
 
  | 
 
將抓出來的 List 透過迴圈並用 addEventListener 來監視假設 input 裡的 value 更換會呼叫方法,方法會印出 HTML 中 input 的 name & value(change在滑動時不會呼叫方法,放開滑鼠後才會執行、要持續的話必須要加上 mousemove )
EventTarget.addEventListener():https://ubin.io/AQGZfT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   | <script>     const inputs = document.querySelectorAll('.controls input')
      inputs.forEach(function(input){       input.addEventListener('change',changeinput)       input.addEventListener('mousemove',changeinput)     })     function changeinput(){       console.log(this.name,this.value)     } 		 		 		      </script>
 
  | 
 
可以印出值之後,透過 switch case 的方式來更換 CSS,如果是抓到 spacing 就要改動其 CSS 中的 padding,抓到 blur 就要改動其CSS 中的 filter 濾鏡,抓到 base 就要改動其 CSS 中的 background,
w3c filter:https://ubin.io/pg51KQ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
   | <script>     
      function changeinput(){              switch (this.name) {         case `spacing`:           document.querySelector('img').style.padding = this.value + 'px'         break;         case `blur`:           document.querySelector('img').style.filter = `blur(${this.value}px)`         break;         case `base`:           document.querySelector('img').style.background = this.value         break;       }     } </script>
 
  | 
 
就大功告成啦!
JS - Final
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
   | <script>     const inputs = document.querySelectorAll('.controls input')
      inputs.forEach(function(input){       input.addEventListener('change',changeinput)       input.addEventListener('mousemove',changeinput)     })
      function changeinput(){              switch (this.name) {         case `spacing`:           document.querySelector('img').style.padding = this.value + 'px'         break;         case `blur`:           document.querySelector('img').style.filter = `blur(${this.value}px)`         break;         case `base`:           document.querySelector('img').style.background = this.value         break;       }     } </script>
 
  | 
 
本次範例程式碼原作者來源:https://reurl.cc/1QgokY