這次要做的不是位移式輪播,而是透明度漸變式的相冊。思路同樣簡單:用兩個變數分別保存「當前圖」與「上一張圖」的索引,再用兩個計時器——一個控制切換圖片的節奏,一個負責逐步調高透明度。
<HTML> <HEAD> <TITLE></TITLE> </HEAD> <style> #cnt{width:100%;height:80%;} .ctrl{text-align:center;border:1px solid gray;font-size:12px;cursor:pointer;} </style> <script defer='defer'> <!-- var curOpac = 0; var filterTimer; var isIE = /internet explorer/i.test(window.navigator.appName); function MyScroll(cnt, control){ this.data = []; // 存放圖片路徑 this.interval = 3000; // 過渡一次的間隔時間(過渡時間+圖片顯示時間) this.timer; // 定時器:控制當前顯示的圖片 this.container = cnt; this.curFrame = 0; this.oldFrame = 0; this.controls = control; // 按鈕集合 Global = this; // 獲取對像的指針 this.run = function(){ this.timer = window.setInterval("Global.showFrame()", this.interval); } // 按鈕的處理程序 this.go = function(i){ curOpac = 0; // 透明度歸0 this.curFrame = i; // 當前要過渡的圖片 this.stop(); // 清空計時器 this.showFrame(); // 當前圖片過渡 this.run(); // 循環播放 } this.stop = function(){ window.clearInterval(this.timer); window.clearInterval(filterTimer); } this.showFrame = function(){ // 設置當前按鈕樣式 this.controls[this.oldFrame].style.backgroundColor = "white"; this.controls[this.curFrame].style.backgroundColor = "gray"; if(isIE) this.container.style.filter = "alpha(opacity=0)"; else this.container.style.cssText = "-moz-opacity:0"; this.container.innerHTML = this.data[this.curFrame]; filterTimer = window.setInterval("blend()", 100); this.oldFrame = this.curFrame; this.curFrame ++; if(this.curFrame == this.data.length){ this.curFrame = 0; } } } // 增加透明度 function blend(){ curOpac+=10; if(isIE) Global.container.style.filter='alpha(opacity=' + curOpac + ')'; else Global.container.style.cssText = "-moz-opacity:" + curOpac/100.0; if(curOpac == 100){ curOpac = 0; window.clearInterval(filterTimer); } } //開始 function startIt(){ var imgArr = []; // 創建4個圖片對像保存圖片路徑 for(var i=0;i<4;i++){ imgArr[i] = new Image(); imgArr[i].src = "images/banner" + (i + 1) + ".jpg"; } var controlArr = $("mainTb").getElementsByTagName("span"); for(var i=0;i<controlArr.length;i++){ controlArr[i].tag = i; controlArr[i].onclick = function(){ myScroll.go(this.tag); } } var myScroll = new MyScroll($("cnt"), controlArr); myScroll.data.push("<img src='" + imgArr[0].src + "'>"); myScroll.data.push("<img src='" + imgArr[1].src + "'>"); myScroll.data.push("<img src='" + imgArr[2].src + "'>"); myScroll.data.push("<img src='" + imgArr[3].src + "'>"); myScroll.go(0); } window.onload = startIt; function $(id){ return document.getElementById(id);} //--> </script> <BODY> <table width="300" height="100" id="mainTb"> <tr> <th rowspan="4"><div id="cnt"> </div></th> <td width="15"><span class="ctrl"> 1 </span></td> </tr> <tr> <td><span class="ctrl"> 2 </span></td> </tr> <tr> <td><span class="ctrl"> 3 </span></td> </tr> <tr> <td><span class="ctrl"> 4 </span></td> </tr> </table> </BODY> </HTML>
這份程式碼是個很典型的「用 JS 手動做動畫」範例,拆解開來有四個要點:
timer 每 3 秒觸發一次換圖,filterTimer 每 100 毫秒把透明度加 10,跑十次剛好完成一次淡入。filter: alpha(opacity=x),其他瀏覽器走 -moz-opacity。這是那個年代的標準寫法,現今統一用 element.style.opacity 即可,程式碼能精簡一半。startIt 先用 new Image() 把四張圖都抓下來,避免切換時才去下載而產生閃白。另外提醒兩處可以改進的地方:一是 setInterval("Global.showFrame()", ...) 用字串形式求值,這在現代瀏覽器中已被強烈不建議(相當於 eval),改成 setInterval(function(){ Global.showFrame(); }, this.interval) 更安全;二是原版宣告了 Global 卻沒有 var,會變成全域變數,多個相冊實例並存時會互相干擾。若要在新專案中使用,建議直接改用 CSS transition: opacity .8s 搭配 setTimeout 切換 class,效果更順、程式碼也更短。
以上就是這次的完整實作,希望對大家學習 JavaScript 動畫有所幫助。