本文為大家展示一個相當有視覺衝擊力的水波文字效果:頁面上的文字會像水面一樣泛起層層波紋,動態起伏。它主要借助瀏覽器的 wave 濾鏡,並通過定時器不斷改變濾鏡的 phase 參數,讓波紋產生流動的錯覺。
效果的關鍵在於 wave 濾鏡的幾個屬性:freq 控制波紋頻率,strength 控制振幅,phase 決定相位。我們只需讓 phase 隨時間遞增,波紋便會持續運動起來。這段代碼沿用了判斷瀏覽器類型的寫法,在支持的環境下才會啟動動畫。
完整源碼如下:
<html>
<head>
<title>JS實現很酷的水波文字效果</title>
</head>
<body bgcolor="#000000" onLoad="if (document.all)wave()">
<center>
<div id='water' style='position:relative;width:400px;height:150px;font-family:Verdana;font-size:50px;color:#8080ff'>
</div>
</center>
<script language="javascript">
<!--
if (document.layers){
alert("你的浏覽器不支持這個特效");
}
else
if (document.all){
var step=3;
var xstep=0;
var msg='歡迎光臨<br>博客園<br>cnblogs.cn';
water.innerHTML=msg
function wave(){
document.all.water.style.filter='wave(freq=3, strength=5, phase='+xstep+', lightstrength=45, add=0, enabled=1)';
xstep+=step;
TIMER=setTimeout('wave()',10);
}
}
//-->
</script>
</html>
希望這份示例能為大家的 JavaScript 學習帶來一點靈感。