DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
百葉窗式畫面拉開的網頁開場動畫
編輯:CSS詳解     

這個特效會在頁面載入時,用八條等寬的直向色塊蓋住整個視窗,接著奇數與偶數條交替收合,像百葉窗或舞台布幕一樣把畫面「拉開」,露出底下的內容。它並非真的光束,而是一種以裁剪(clip)方式製造的揭幕動畫,開場效果相當俐落。

實作重點

程式為每個 intro 的 DIV 動態計算寬度(視窗寬度除以八),奇數與偶數分別改變裁剪區域的上下邊界,再用 setInterval 每 100 毫秒推進一次,直到裁剪範圍歸零便停止。核心概念是「用一層蓋住的方塊,逐步縮小可視範圍」。

<!-- 第一步:把樣式加入 <head> 區段 -->
<style>
.intro{position:absolute; left:0; top:0; background-color:green; border:0.1px solid green}
</style>

<!-- 第二步:把下列區塊與程式碼加入 <body> 區段 -->
<div id="i1" class="intro"></div>
<div id="i2" class="intro"></div>
<div id="i3" class="intro"></div>
<div id="i4" class="intro"></div>
<div id="i5" class="intro"></div>
<div id="i6" class="intro"></div>
<div id="i7" class="intro"></div>
<div id="i8" class="intro"></div>

<SCRIPT language=Javascript>
var speed = 30
var temp = new Array()
var temp2 = new Array()
if (document.layers) {
  for (i = 1; i <= 8; i++) {
    temp[i] = eval("document.i" + i + ".clip")
    temp2[i] = eval("document.i" + i)
    temp[i].width = window.innerWidth / 8 - 0.3
    temp[i].height = window.innerHeight
    temp2[i].left = (i - 1) * temp[i].width
  }
} else if (document.all) {
  var clipbottom = document.body.offsetHeight, cliptop = 0
  for (i = 1; i <= 8; i++) {
    temp[i] = eval("document.all.i" + i + ".style")
    temp[i].width = document.body.clientWidth / 8
    temp[i].height = document.body.offsetHeight
    temp[i].left = (i - 1) * parseInt(temp[i].width)
  }
}
function openit() {
  window.scrollTo(0, 0)
  if (document.layers) {
    for (i = 1; i <= 8; i = i + 2) temp[i].bottom -= speed
    for (i = 2; i <= 8; i = i + 2) temp[i].top += speed
    if (temp[2].top > window.innerHeight) clearInterval(stopit)
  } else if (document.all) {
    clipbottom -= speed
    for (i = 1; i <= 8; i = i + 2) {
      temp[i].clip = "rect(0 auto+" + clipbottom + " 0)"
    }
    cliptop += speed
    for (i = 2; i <= 8; i = i + 2) {
      temp[i].clip = "rect(" + cliptop + " auto auto)"
    }
    if (clipbottom <= 0) clearInterval(stopit)
  }
}
function gogo() {
  stopit = setInterval("openit()", 100)
}
gogo()
</SCRIPT>

若想改成其他顏色的布幕,只要調整 .intro 的 background-color;想加快或放慢揭幕速度,則可增減 speed 與 setInterval 的間隔毫秒數。

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