這次做了兩個小特效:一個基於原生 input[type=range],一個完全自定義。完整程式碼如下,貼進瀏覽器就能看到效果。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style> #tip{ position: absolute; top: 30px; left: 0; right: 0; width: 200px; height: 160px; margin: auto; border: 1px solid gray; background-color: cornsilk; } #tip div{ position: relative; width: 100%; height: 80px; border-bottom: 1px solid gray; } .out{ position: relative; left: 16%; display: inline-block; border: 2px solid royalblue; margin-top: 20px; width: 130px; height: 20px; background-color: lightgoldenrodyellow; } .in{ display: block; height: 20px; line-height: 20px; text-align: right; color: white; width: 50%; background-image: linear-gradient(to right,powderblue 0%,#336699 50%,red 100%); -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } input[type="range"] { position: relative; left: 19%; top: 5px; box-shadow: 0 1px 0 0px #424242, 0 1px 0 #060607 inset, 0px 2px 10px 0px black inset, 1px 0px 2px rgba(0, 0, 0, 0.4) inset, 0 0px 1px rgba(0, 0, 0, 0.6) inset; background-color: lightskyblue; border-radius: 15px; width: 60%; -webkit-appearance: none; -moz-appearance: none; appearance: none; height:15px; } input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; -moz-appearance: none; appearance: none; height: 20px; width: 10px; background-color: coral; border-radius: 15px; -webkit-box-shadow: 0 -1px 1px black inset; -moz-box-shadow: 0 -1px 1px black inset; box-shadow: 0 -1px 1px black inset; } input[type="range"]:before{ content: attr(value); color: white; border-radius: 5px 0 0 5px; background-color: lightskyblue; } input[type="range"]:after{ content: attr(max); color: white; border-radius:0 5px 5px 0; background-color: lightskyblue; } .b{ display: inline-block; width: 22px; padding: 0; } #outer2{left: 5px} #btn1{ position: relative; left: 5px; } #btn2{ position: relative; left: 5px; } </style> <script> window.onload = function(){ //原生組件range var inner = document.getElementById('inner1'); var range = document.getElementById('range'); range.onclick = function(){ inner.innerHTML = range.value; inner.style.width = range.value+'%'; }; range.onmousemove = function(){ inner.innerHTML = range.value; inner.style.width = range.value+'%'; }; //自定義組件 var outer2 = document.getElementById('outer2'); var inner2 = document.getElementById('inner2'); var btn1 = document.getElementById('btn1'); var btn2 = document.getElementById('btn2'); var id,id1; var value = parseInt(inner2.innerHTML); var a = parseFloat(window.getComputedStyle(outer2,null).width)/100; //減--- btn1.onmousedown = function(){ id1 = setTimeout(function change(){ if(value>0) { value--; inner2.innerHTML = value; inner2.style.width = (value) * a + 'px'; id = setTimeout(function(){ clearTimeout(id); change(); },16.7); }else{clearTimeout(id);} },500); }; btn1.onmouseup = function(){clearTimeout(id1);clearTimeout(id)}; btn1.onclick = function(){ console.log('a:'+a+','+'value:'+value); if(value>0){ value--; inner2.style.width = (value)*a+'px'; } }; //加+++ btn2.onmousedown = function(){ id1 = setTimeout(function change(){ if(value<100) { value++; inner2.innerHTML = value; inner2.style.width = value * a + 'px'; id = setTimeout(function(){ clearTimeout(id); change(); },16.7); }else{clearTimeout(id);} },500); }; btn2.onmouseup = function(){clearTimeout(id1);clearTimeout(id)}; btn2.onclick = function(){ if(value<100){ value++; inner2.innerHTML = value; inner2.style.width = value*a+'px'; } } } </script> </head> <body> <form id="tip"> <div> <span id="outer1" class="out"> <span id="inner1" class="in">50</span> </span> <input id="range" class="ran" type="range" min="0" max="100" step="1" value="50"> </div> <div id="d2"> <input id="btn1" class="b" type="button" value="<"> <span id="outer2" class="out"> <span id="inner2" class="in">50</span> </span> <input id="btn2" class="b" type="button" value=">"> </div> 按住按鈕0.5秒, 會持續變化! </form> </body> </html>
第一個實作很簡單就不解釋了,看程式碼即可——本質上只是把 range 的值同步寫進內層 span 的文字與寬度。
看到一個需求或別人的特效時,別急著翻程式碼。先想:如果是我,會怎麼做?把思路整理出來再動手。
實現原理:
1. 一個 span 內再嵌一個 span。外層只負責顯示寬、高與邊框,背景透明;內層高度與外層相同,寬度預設 50%,並事先鋪好線性漸變背景。改內層的寬度,視覺上就是進度條在動。
2. 按鈕的 onclick 很直觀:按一下就改變內層 span 的寬度與顯示數字。
3. 長按的連續變化:onmousedown 啟動計時器,等 500 毫秒後開始執行 change,而 change 是一個用 setTimeout 呼叫自己的函式,每 16.7 毫秒反覆一次(約合 60fps),於是形成連續動畫。
1. 取得 1% 的像素寬度。var a = parseFloat(window.getComputedStyle(outer2,null).width)/100;——用來換算「每個百分比對應幾像素」。若寫成 outer2.style.width 是讀不到值的,因為那樣式並沒有寫在行內。當然也可以直接寫死 var a = 1.3。要注意 IE9 以下不支援 getComputedStyle,那時得用 element 的 currentStyle 屬性。
2. onmouseup 這一行很關鍵。btn1.onmouseup = function(){clearTimeout(id1);clearTimeout(id)};——少了它,onclick 觸發之前會先觸發 onmousedown,500 毫秒後長按的迴圈就啟動了,之後會一直跑下去停不下來。這行的作用是在放開滑鼠的那一刻把兩層計時器都清掉,讓後續的 onclick 只走「單步」邏輯。
3. 其餘部分都不是難點。
這個「外框固定、內層寬度可變」的結構其實很通用。把中間的顯示區換成文章、圖片、影片播放進度、音量控制,再把左右按鈕換成自定義樣式,馬上就是另一個元件。理解它之後,實作任何「連續可調量」的 UI 都有同一套骨架:一個狀態變數、一個渲染函式、一組改變狀態的輸入。
若要改良現有程式碼,有幾點值得注意。16.7 毫秒的 setTimeout 應改用 requestAnimationFrame,它會對齊螢幕刷新率、在分頁隱藏時自動暫停,比固定間隔的計時器準得多。寬度變化會觸發重排,改用 transform: scaleX() 搭配 transform-origin: left 能交給合成器處理,動畫更順。狀態存進 DOM 文字節點(parseInt(inner2.innerHTML))在正式專案中應改為獨立變數,視圖只負責呈現。按鈕應加上 aria-label,並考慮鍵盤操作(方向鍵微調)與觸控長按的 touchstart/touchend 對應。
如果覺得哪裡寫得不好,歡迎指出。以上就是這次的兩個特效實作,希望能給同樣在練原生 JS 的人一點參考。