鼠标滑过图片透明度发生改变的特效

这个特效感觉不错,把代码发出来共享一下!

此特效实现了网页图片半透明特效,把鼠标放上后可以改变透明度!

第一步:在网页中加入以下脚本代码:

Javascript 代码
 
<script language=javascript>
 nereidFadeObjects = new Object();
 nereidFadeTimers = new Object();
 function nereidFade(object, destOp, rate, delta){
  if (!document.all)
  return
  if (object != "[object]"){
   setTimeout("nereidFade("+object+","+destOp+","+rate+","+delta+")",0);
   return;
  }
  clearTimeout(nereidFadeTimers[object.sourceIndex]);
  diff = destOp-object.filters.alpha.opacity;
  direction = 1;
  if (object.filters.alpha.opacity > destOp){
   direction = -1;
  }
  delta=Math.min(direction*diff,delta);
  object.filters.alpha.opacity+=direction*delta;
  if (object.filters.alpha.opacity != destOp){
   nereidFadeObjects[object.sourceIndex]=object;
   nereidFadeTimers[object.sourceIndex]=setTimeout("nereidFade(nereidFadeObjects["+object.sourceIndex+"],"+destOp+","+rate+","+delta+")",rate);
  }
 }
</script>

第二步:在显示图片标签<img>里添加如下代码:

onMouseOut="nereidFade(this,40,10,2)" onMouseOver="nereidFade(this,100,10,2)" style="FILTER: alpha(opacity=10)"

完整代码如下:

<img src="https://yangjunwei.com/images/logo.gif" border=0 onMouseOut="nereidFade(this,40,10,2)" onMouseOver="nereidFade(this,100,10,2)" style="FILTER: alpha(opacity=10)">

其中第二个参数是透明度,opacity的值也是控制不透明度的。

最后,给个代码演示:演示Demo