经常看到网页里图片渐变显示,自己写一个。 原理很简单就是修改元素的css透明度。 在线预览效果:http://jsfiddle.net/dtdxrk/BHUp9/embedded/result/ 复制代码 代码如下: 原生Js元素渐隐/渐显方法 渐显 渐隐 <BR>function alphaPlay(obj,method){ //method有两个值show或hiden <BR>var n = (method == "show") ? 0 : 100, <BR>ie = (window.ActiveXObject) ? true : false; <BR>var time = setInterval(function(){ <BR>if(method == "show"){ <BR>if(n < 100){ <BR>n+=10; <BR>if(ie){ <BR>obj.style.cssText = "filter:alpha(opacity="+n+")"; <BR>}else{ <BR>(n==100) ? obj.style.opacity = 1 : obj.style.opacity = "0."+n; <BR>} <BR>}else{ <BR>clearTimeout(time); <BR>} <BR>}else{ <BR>if(n > 0){ <BR>n-=10; <BR>if(ie){ <BR>obj.style.cssText = "filter:alpha(opacity="+n+")"; <BR>}else{ <BR>obj.style.opacity = "0."+n; <BR>} <BR>}else{ <BR>clearTimeout(time); <BR>} <BR>} <BR>},30); <BR>} <BR>var img = document.getElementById("img"); <BR>document.getElementById("show").onclick = function(){ <BR>alphaPlay(img,"show"); <BR>} <BR>document.getElementById("hiden").onclick = function(){ <BR>alphaPlay(img,"hiden"); <BR>} <BR>