DIV CSS 佈局教程網

圖片漸變切換效果:從 IE Filter 到 CSS Transition 的實作演進
編輯:關於JavaScript     

圖片輪播加上淡入淡出,是首頁 Banner 的標準配備。本文先看一份使用 IE 專有 filter 的古老寫法,理解它怎麼運作,再用現代 CSS 重寫一遍——兩者對照,剛好能說明 Web 標準演進的軌跡。

舊寫法:GradientWipe 滤镜

Internet Explorer 曾提供 filter 這套 DirectX 視覺效果,其中 GradientWipe 能以漸層擦除的方式切換內容。核心觀念是:呼叫 Apply() 準備、play() 播放,並在播放前切換 display 決定哪張圖要出現。

<style type="text/css">
.woon  { border: 1px solid #fff; }
.wooff { border: 1px solid #ffffff;
         filter: progid:DXImageTransform.Microsoft.Alpha(style=1,opacity=150,finishOpacity=1); }
</style>

<div id="fc"
  style="filter:progid:DXImageTransform.Microsoft.GradientWipe(duration=3,gradientSize=0.5,motion=forward);
         width:500px; height:260px; border:1px solid #99cc66">

  <div style="display:block">
    <a href="#" target="_blank"><img src="1.jpg" width="500" height="300" border="0"
      onmouseover="clearAuto()" onmouseout="setAuto()"></a>
  </div>
  <div style="display: none">
    <a href="#" target="_blank"><img src="2.jpg" width="500" height="300" border="0"
      onmouseover="clearAuto()" onmouseout="setAuto()"></a>
  </div>
  <div style="display: none">
    <a href="#" target="_blank"><img src="3.jpg" width="500" height="300" border="0"
      onmouseover="clearAuto()" onmouseout="setAuto()"></a>
  </div>
</div>

底下搭配縮圖作為導覽:

<table border="0" align="center" cellpadding="0" cellspacing="1" id="num">
  <tr>
    <td class="woon"  onmouseover="clearAuto()" onclick="Mea(0)" onmouseout="setAuto()">
      <img src="1.jpg" width="120" height="70" style="cursor:pointer"></td>
    <td class="wooff" onmouseover="clearAuto()" onclick="Mea(1)" onmouseout="setAuto()">
      <img src="2.jpg" width="120" height="70" style="cursor:pointer"></td>
    <td class="wooff" onmouseover="clearAuto()" onclick="Mea(2)" onmouseout="setAuto()">
      <img src="3.jpg" width="120" height="70" style="cursor:pointer"></td>
  </tr>
</table>

腳本部分(已修正原始程式碼中 getElementById 與 getElementsByTagName 被寫壞的方法名):

<script>
var n = 0;
var showNum = document.getElementById("num");

function Mea(value) {
  n = value;
  setBg(value);
  plays(value);
}

// 切換縮圖高亮狀態
function setBg(value) {
  for (var i = 0; i < 4; i++) {
    showNum.getElementsByTagName("td")[i].className = (value == i) ? 'woon' : 'wooff';
  }
}

// 播放漸變並切換圖片
function plays(value) {
  with (fc) {
    filters[0].Apply();
    for (var i = 0; i < 4; i++) {
      children[i].style.display = (i == value) ? "block" : "none";
    }
    filters[0].play();
  }
}

function clearAuto() { clearInterval(autoStart); }
function setAuto()   { autoStart = setInterval("auto()", 5000); }

function auto() {
  n++;
  if (n > 3) n = 0;
  Mea(n);
}
function sub() {
  n--;
  if (n < 0) n = 3;
  Mea(n);
}
setAuto();
</script>

這份程式碼今天已經完全不能運作:IE 的 filter 從 IE10 起被移除,with 陳述式在嚴格模式下被禁用,style="cursor: hand" 也早已不是合法值。

現代重寫:opacity + transition

同樣的效果,用 CSS3 只要三行:

<style>
.slideshow {
  position: relative;
  width: 500px;
  height: 300px;
  overflow: hidden;
}
.slideshow img {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  transition: opacity 1s ease-in-out;   /* 淡入淡出 */
}
.slideshow img.active {
  opacity: 1;
  transition: opacity 1s ease-in-out;   /* 只有 active 的才「進場」 */
  z-index: 1;
}
</style>

<div class="slideshow">
  <img src="1.jpg" class="active" alt="第一張">
  <img src="2.jpg" alt="第二張">
  <img src="3.jpg" alt="第三張">
</div>

<script>
(function () {
  var slides = document.querySelectorAll('.slideshow img');
  var idx = 0;
  var timer;

  function show(next) {
    slides[idx].classList.remove('active');
    idx = (next + slides.length) % slides.length;
    slides[idx].classList.add('active');
  }

  function start() { timer = setInterval(function () { show(idx + 1); }, 3000); }
  function stop()  { clearInterval(timer); }

  var box = document.querySelector('.slideshow');
  box.addEventListener('mouseenter', stop);
  box.addEventListener('mouseleave', start);

  // 鍵盤可操作:左右方向鍵切換
  box.setAttribute('tabindex', '0');
  box.addEventListener('keydown', function (e) {
    if (e.key === 'ArrowRight') { stop(); show(idx + 1); }
    if (e.key === 'ArrowLeft')  { stop(); show(idx - 1); }
  });

  start();
})();
</script>

為什麼新版比較好

  • 用 class 切換取代 display:display 無法被 transition 動畫化(它沒有中間值),這正是舊寫法必須借重 filter 的根本原因。opacity 可以。
  • GPU 加速:opacity 與 transform 能被合成層處理,動畫不佔用主執行緒,比 filter 的軟體運算快得多。
  • 去掉了 with:with 讓作用域變得不可預測,是嚴格模式明文禁止的語法。
  • 事件回呼用函式參照而非字串:setInterval("auto()", 5000) 相當於一次 eval,有安全與效能疑慮,改成 setInterval(fn, 5000)。
  • 滑鼠移入自動暫停、鍵盤可操作:這是無障礙的標準要求,舊版完全沒有。

如果你今天要實作輪播,除了上述手寫版,也可以考慮 CSS scroll-snap 搭配 scroll-behavior 的極簡方案,或直接採用成熟套件。重點是:凡是以 IE filter 為基礎的視覺效果,都應該視為需要重寫的技術債。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved