DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
用 revealTrans 濾鏡製作絢麗的圖片輪播效果
編輯:CSS詳解     

CSS 中的 revealTrans 濾鏡在製作網頁特效上很有用處。這次我們用它來做一個帶轉場動畫的圖片輪播,而且每張圖片各自有對應的連結地址。

實作思路:以 revealTrans 濾鏡產生切換動畫,圖片輪播的同時,點擊可導向該張圖片設定的頁面。

步驟一:在 <head> 中插入 JS

<script language="JavaScript1.2">
function reapply() {
  setTimeout("slideit()", 800)
  return true
}
window.onerror = reapply
</script>

<script language="JavaScript1.1">
<!--
// preload images
var image1 = new Image()
image1.src = "images/圖片地址1"
var image2 = new Image()
image2.src = "images/圖片地址2"
var image3 = new Image()
image3.src = "images/圖片地址3"
var image4 = new Image()
image4.src = "images/圖片地址4"
var image5 = new Image()
image5.src = "images/圖片地址5"
var image6 = new Image()
image6.src = "images/圖片地址6"
var image7 = new Image()
image7.src = "images/圖片地址7"
var image8 = new Image()
image8.src = "images/圖片地址8"
var image9 = new Image()
image9.src = "images/圖片地址9"
//-->
</script>

其中 images/圖片地址 為顯示圖片的路徑,可依實際情況修改。預先載入圖片這一步不能省,否則第一次切換會因為圖片還在抓取而出現空白閃屏。

步驟二:在 <body> 啟動

<body onload="slideit()">

步驟三:插入圖片並掛上濾鏡

<img src="images/圖片地址1" width="120" height="90" border="0"
     style="FILTER: revealTrans(Duration=2, Transition=23)" name="slide">

兩個參數的意義:Duration 是轉場持續的秒數,這裡是 2 秒;Transition 是轉場類型編號,23 對應隨機溶解。常見值還有 0(矩形收合)、1(矩形展開)、12(溶解)等,可以替換試效果。name="slide" 則是給後續程式碼抓取的識別名稱。

步驟四:為圖片加上超連結

<a href="JavaScript:slidelink()"><img ... ></a>

步驟五:輪播與導向邏輯

<script>
<!--
// 修改圖片張數
var number_of_images = 9
// 修改速度(秒)
var speed = 3
var step = 1
var whichimage = 1

function slideit() {
  if (!document.images)
    return
  if (document.all)
    slide.filters.item(0).Apply()
  document.images.slide.src = eval("image" + step + ".src")
  if (document.all)
    slide.filters.item(0).Play()
  whichimage = step
  if (step < number_of_images)
    step++
  else
    step = 1
  if (document.all)
    setTimeout("slideit()", speed * 1000 + 3000)
  else
    setTimeout("slideit()", speed * 1000)
}

function slidelink() {
  if (whichimage == 1) top.location.href = "路徑/index.html"
  if (whichimage == 2) top.location.href = "路徑/news.html"
  if (whichimage == 3) top.location.href = "路徑/game.html"
  if (whichimage == 4) top.location.href = "路徑/music.html"
  if (whichimage == 5) top.location.href = "路徑/sport.html"
  if (whichimage == 6) top.location.href = "路徑/radio.html"
  if (whichimage == 7) top.location.href = "路徑/travel.html"
  if (whichimage == 8) top.location.href = "路徑/card.jsp"
  else if (whichimage == 9) top.location.href = "路徑/study.html"
}
//-->
</script>

注意:各連結地址必須與 <head> 中預載的圖片一一對應,頁面數量也要與圖片張數相符。

這段程式在教我們什麼

revealTrans 的核心概念其實很值得記住:先 Apply、換圖、再 Play。Apply 告訴瀏覽器「準備一個轉場」,中間換掉圖片來源,Play 才真正把動畫播出來。這個三段式流程,跟今天 Web Animations API 裡「定義動畫 → 變更狀態 → 播放」的思路如出一轍。

不過必須坦白說,FILTER: revealTrans 是 IE 私有特性,其他瀏覽器一律忽略,因此這份程式在非 IE 環境下只會變成沒有動畫的靜態換圖(程式碼中的 document.all 判斷就是在處理這件事)。

放在今天,同樣的效果用 CSS transition 加 opacity 只要三行:

.slide {
  transition: opacity 2s;
}

再搭配 setTimeout 或 setInterval 切換 class 即可,跨瀏覽器一致、效能更好,也不必預載 hack。另外提醒兩點:輪播不宜過快以免妨礙閱讀,且若有自動播放,應提供暫停控制——這既是無障礙規範的要求,也是對使用者注意力的尊重。

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