DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
像拉開窗簾一樣的開場動畫:用 clip 讓畫面上下一分為二退場
編輯:CSS詳解     

有些網站進入時會有一個短暫的開場:一整片色塊蓋住畫面,然後從中間往上下裂開、退場,露出底下的內容。這份範例用兩個填滿半個視窗的 div 加上 clip 屬性,做出完全相同的效果,全程沒有圖片、沒有 Flash。

原理:用 clip 裁掉一半

clip: rect(top, right, bottom, left) 定義一個矩形可視區域,落在區域外的部分被裁掉。程式的做法是放兩個色塊,上面那塊蓋住 0 到 50% 的高度,下面那塊蓋住 50% 到 100%,然後隨時間把上塊的 bottom 往上收、下塊的 top 往下推,兩塊就同時往兩側縮退去場。

原始代碼

<!-- 把如下代碼加入到 <body> 區域中 -->
<body>
<div id="i1" class="intro"></div>
<div id="i2" class="intro"></div>

<script language="JavaScript1.2">
var speed = 20;
var temp  = new Array();
var temp2 = new Array();

if (document.layers){
  for (i = 1; i <= 2; i++){
    temp[i]  = eval("document.i" + i + ".clip");
    temp2[i] = eval("document.i" + i);
    temp[i].width  = window.innerWidth;
    temp[i].height = window.innerHeight / 2;
    temp2[i].top   = (i - 1) * temp[i].height;
  }
} else if (document.all){
  var clipbottom = document.body.offsetHeight / 2, cliptop = 0;
  for (i = 1; i <= 2; i++){
    temp[i] = eval("document.all.i" + i + ".style");
    temp[i].width  = document.body.clientWidth;
    temp[i].height = document.body.offsetHeight / 2;
    temp[i].top    = (i - 1) * parseInt(temp[i].height);
  }
}

function openit(){
  window.scrollTo(0, 0);
  if (document.layers){
    temp[1].bottom -= speed;
    temp[2].top    += speed;
    if (temp[1].bottom <= 0) clearInterval(stopit);
  } else if (document.all){
    clipbottom -= speed;
    temp[1].clip = "rect(0 auto " + clipbottom + " 0)";
    cliptop += speed;
    temp[2].clip = "rect(" + cliptop + " auto auto)";
    if (clipbottom <= 0) clearInterval(stopit);
  }
}

function gogo(){ stopit = setInterval("openit()", 100); }
gogo();
</script>

<style>
.intro {
  position: absolute; left: 0; top: 0;
  layer-background-color: Aqua; background-color: Aqua;
  border: 0.1px solid Aqua;
}
</style>
</body>

幾個聰明的地方

用 parseInt 修正型別。 設定 height 時瀏覽器會自動加上 px,讀回來是 "300px" 這樣的字串;直接拿去做乘法會發生隱式轉換,在同層計算下容易出錯,所以第二次迭代時用 parseInt(temp[i].height) 明確取整數。這個細節很多仿作都漏掉了。

用 clearInterval 自我終止。 當裁切高度歸零就主動清掉計時器,不會留下永遠空轉的迴圈——這一點比同期的很多範例都做得好。

window.scrollTo(0, 0)。 確保動畫開始時頁面在頂端,避免色塊位置與視窗錯位。

兩個時代限制

document.layers 與 document.all 這兩條分支現在都不需要了;layer-background-color 是 Netscape 專屬屬性。而 clip: rect() 這個舊語法已被 clip-path 取代,且舊語法只對 position: absolute 的元素有效,這也是原範例必須用絕對定位的原因。

現代寫法

<div class="curtain">
  <div class="half top"></div>
  <div class="half bottom"></div>
</div>

<style>
.curtain { position: fixed; inset: 0; z-index: 9; pointer-events: none; }
.half { position: absolute; left: 0; width: 100%; height: 50.5%;
        background: #0ff; transition: transform 1s ease-in-out; }
.half.top    { top: 0; }
.half.bottom { bottom: 0; }
.curtain.open .half.top    { transform: translateY(-100%); }
.curtain.open .half.bottom { transform: translateY(100%); }
</style>

<script>
window.addEventListener('load', function () {
  requestAnimationFrame(function () {
    document.querySelector('.curtain').classList.add('open');
  });
});
</script>

改用 transform 的動畫只會觸發合成層,不會重排頁面,效能遠優於逐格改 clip;fixed 定位讓色塊不受捲動影響;pointer-events: none 確保退場後不會擋住底下的操作。最後記得包一層:

@media (prefers-reduced-motion: reduce) {
  .half { transition: none; }
}

開場動畫很容易變成「每次進站都得多等一秒」的負擔,尊重系統的減少動態偏好,是現在做這類效果的基本禮貌。

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