DIV CSS 佈局教程網

用原生 JavaScript 實作仿 Flash 遮罩開場動畫
編輯:關於JavaScript     

Flash 退出歷史舞台之後,當年那種「九宮格逐步揭幕」的經典遮罩動畫,其實用 JavaScript 加上一點絕對定位就能完整復刻。本文提供一份可直接運行的完整範例,適合用在首頁 banner 或活動頁的開場效果。

效果原理

把容器切成 3×3 共九塊遮罩,中央那一塊保持透明作為「窗口」,其餘八塊填色。隨著時間推進,中央窗口以某個隨機位置為中心不斷放大,九塊遮罩跟著重算寬高與座標;當四角遮罩都被擠到尺寸歸零,動畫即結束並清除所有遮罩節點。

寬高增長速度依容器長寬比做加權,這樣橫幅與直幅都能有自然的擴散觀感。

完整程式碼

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>仿 Flash 遮罩動畫</title>
<script charset="utf-8" src="jquery.js"></script>
<style media="screen">
  body { margin: 0; }
  #banner { position: relative; width: 858px; height: 238px; overflow: hidden; }
</style>
</head>
<body>
<div id="banner">
  <a href="javascript:void(0);"><img src="1.jpg" width="858" height="238"/></a>
</div>
<script type="text/javascript">
function setMaskingAnimation(container, width, height, time, pixel, color) {
  // 隨機決定擴散中心,避開最外圈,讓動畫不至于一開始就結束
  var __left = mtRand(parseInt(width * 0.25), parseInt(width * 0.75));
  var __top  = mtRand(parseInt(height * 0.25), parseInt(height * 0.75));

  var widthSpeed, heightSpeed, __width, __height;
  if (width >= height) {
    widthSpeed  = parseInt((width / height) * 10);
    heightSpeed = 10;
  } else {
    widthSpeed  = 10;
    heightSpeed = parseInt((height / width) * 10);
  }
  __width  = widthSpeed;
  __height = heightSpeed;

  var hander;

  // 依中央窗口的位置與尺寸,計算九塊遮罩各自的座標
  function getPosition(_width, _height, _left, _top) {
    var div1 = { "width": _left, "height": _top, "left": 0, "top": 0 };
    var div2 = { "width": _width, "height": _top, "left": _left, "top": 0 };
    var div3 = { "width": width - _left - _width, "height": _top, "left": _left + _width, "top": 0 };
    var div4 = { "width": _left, "height": _height, "left": 0, "top": _top };
    var div5 = { "width": _width, "height": _height, "left": _left, "top": _top };
    var div6 = { "width": width - _left - _width, "height": _height, "left": _left + _width, "top": _top };
    var div7 = { "width": _left, "height": height - _top - _height, "left": 0, "top": _top + _height };
    var div8 = { "width": _width, "height": height - _top - _height, "left": _left, "top": _top + _height };
    var div9 = { "width": width - _left - _width, "height": height - _top - _height, "left": _left + _width, "top": _top + _height };
    return { div1: div1, div2: div2, div3: div3, div4: div4, div5: div5,
             div6: div6, div7: div7, div8: div8, div9: div9 };
  }

  // 閉區間隨機整數
  function mtRand(n1, n2) {
    return parseInt(Math.random() * (n2 - n1 + 1) + n1);
  }

  // 不存在就建立,已存在就更新樣式
  function setDiv(i, position) {
    var has = $(container).find("div.mask" + i);
    if (has.length) {
      has.css({ "left": position.left, "top": position.top,
                "width": position.width, "height": position.height });
    } else {
      var html = '<div class="mask mask{@i}" style="position:absolute;left:{@left}px;top:{@top}px;width:{@width}px;height:{@height}px;background-color:{@backgroundColor};"></div>';
      html = html.replace('{@i}', i)
                 .replace('{@left}', position.left)
                 .replace('{@top}', position.top)
                 .replace('{@width}', position.width)
                 .replace('{@height}', position.height)
                 .replace('{@backgroundColor}', i == 5 ? "transparent" : color);
      $(container).append(html);
    }
  }

  function play() {
    __width  += pixel * widthSpeed;
    __height += pixel * heightSpeed;
    __left   -= pixel * widthSpeed / 2;
    __top    -= pixel * heightSpeed / 2;

    var position = getPosition(__width, __height, __left, __top);
    for (var i = 1; i <= 9; i++) {
      setDiv(i, position["div" + i]);
    }

    // 四角都被吃光,動畫收尾
    if (position.div1.width <= 0 && position.div1.height <= 0 &&
        position.div9.width <= 0 && position.div9.height <= 0) {
      window.clearInterval(hander);
      $(container).find("div.mask").remove();
    }
  }

  hander = window.setInterval(play, time);
}

$(function () {
  setMaskingAnimation("#banner", 858, 238, 100, 2, "#ff0000");
  // 第 4、5 個參數分別設為 20 與 2 時視覺效果較佳;第 5 個參數需為偶數
});
</script>
</body>
</html>

參數怎麼調

  • time:輪播計時器的間隔毫秒數,數值越小動畫越快。
  • pixel:每次位移的步長,決定擴散的「粒度」,實測 2 這個偶數值畫面最順。
  • color:遮罩顏色,可換成品牌色或直接設為 transparent 做純淡入效果。

這份實作只依賴 jQuery 做節點操作,邏輯本身與框架無關,若想改寫成原生版本,把 $().find/css/append 替換成 querySelectorAll 與 style 賦值即可,並建議改用 requestAnimationFrame 取代 setInterval,畫面會更順且不浪費資源。

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