标签:
CSS相关透明度的设置方式
filter:alpha(opacity=50);
opacity: 0.5;
opacity: 0.5
This is the “most important” one because it is the current standard in CSS. This will work in most versions of Firefox, Safari, and Opera. This would be all you need if all browsers supported current standards. Which, of course, they don’t. filter: alpha(opacity=50)
This one you need for IE.
function setOpacity(elem, opacity) { /* * elem : The id of the element; * opacity: The value of alpha, which is a decimals. */ if(elem.style.filter) { //IE elem.style.filter = ‘alpha(opacity:‘ + opacity * 100 + ‘)‘; } else { elem.style.opacity = opacity; } } /* function getOpacity(elem) { return (elem.style.filter ? elem.style.filter) }*/ function fadeIn(elem, speed) { /* * elem, the id of the element; * speed, the speed for the fadeIn.(The value lower, the less time needs) * opacity, the target opacity will be reach, 0.0 to 1.0 */ elem.style.display = "block"; setOpacity(elem, 0); var tempOpacity = 0; (function(){ setOpacity(elem, tempOpacity); tempOpacity += 0.05; if(tempOpacity <= 1) { setTimeout(arguments.callee, speed); //tempOpacity += 0.05; } })(); } function fadeOut(elem, speed) { /* * elem, the id of the element; * speed, the speed for the fadeout; speed, the speed for the fadein.(The value lower, the less time needs); */ elem.style.display="block"; var tempOpacity = 1; (function(){ setOpacity(elem, tempOpacity); tempOpacity -= 0.05; if(tempOpacity > 0) { setTimeout(arguments.callee, speed); console.log("why"); } else { elem.style.display = "none"; //不可放在匿名函数外面会先执行。 } })(); //elem.style.display = "none"; } function fadeTo(elem, speed, opacity){ /* elem, the id of the element; * speed, the speed to of the fadeTo.(The value lower, the less time needs) * opacity, the opacity of the final result; */ var tempOpacity = 0; elem.style.display = "block"; (function(){ setOpacity(elem, tempOpacity); tempOpacity += 0.05; if(tempOpacity <= opacity) { setTimeout(arguments.callee, speed); } })(); }
tempOpacity
的函数fadeOut出栈了,但是后来的匿名还是可以访问到这个变量。这主要是因为虽然fadeOut函数返回了,其执行环境的作用域链被销毁了, 但是其活动对象(由每个函数的arguments和其他命名参数来初始化得到的)仍然会留在内在中,因为匿名函数中有引用到这个对象(js的内存管理是使用垃圾回收机制的),所以匿名函数也就可以使用这个对象里面的变量了,只有当匿名函数被销毁时,其活动对象才会被销毁。标签:
原文地址:http://www.cnblogs.com/kinthon/p/4864960.html