hasLayout为
false的元素的透明度时在IE8的标准模式下是失败的,这个bug在YUI(我已经给YUI团队提交了这个bug,他们会在下个版本修复,最新的 2.8.0中依旧存在,期待2.9.0吧)、Prototype、jQuery和Mootools的最新版本中都存在,具体请在IE8标准模式下看demo9到demo11。同样由于在IE8中设置透明度的方式多种多样,所以利用JavaScript获取HTML元素的透明度值需要考虑多种情况,YUI完美解决了这个问题,Prototype比jQuery稍微周全一点,而Mootools直接是bug,具体可以在IE下看demo1到demo8的演示。从这个角度给4个框架来个排名的话,YUI第一、 Prototype第二、jQuery第三、Mootools垫底。我简单的实现了设置和获取Opacity的函数,可以避开上面框架存在的bug,请在IE8标准模式下看demo12:
复制代码
//设置CSS opacity 属性的函数,解决IE8的问题
var setOpacity = function(el,i){
if(window.getComputedStyle){// for non-IE
el.style.opacity = i;
}else if(document.documentElement.currentStyle){ // for IE
if(!el.currentStyle.hasLayout){
el.style.zoom = 1;
}
if(!el.currentStyle.hasLayout){ //在IE8中zoom不生效,所以再次设置inline-block
el.style.display = ‘inline-block’;
}
try{
//测试是否已有filter
//http://msdn.microsoft.com/en-us/library/ms532847%28VS.85%29.aspx
if(el.filters){
if(el.filters(‘alpha’)){
el.filters(‘alpha’).opacity = i * 100;
}else{
el.style.filter += ‘alpha(opacity=’+ i * 100 +’)’;
}
}
}catch(e){
el.style.filter = ‘alpha(opacity=’+ i * 100 +’)’;
}
}
}
//获取CSS opacity 属性值的函数
//借鉴自http://developer.yahoel.com/yui/docs/YAHOO.util.Dom.html#method_getStyle
var getOpacity = function(el){
var value;
if(window.getComputedStyle){
value = el.style.opacity;
if(!value){
value = el.ownerDocument.defaultView.getComputedStyle(el,null)[‘opacity’];
}
return value;
}else if(document.documentElement.currentStyle){
value = 100;
try { // will error if no DXImageTransform
value = el.filters[‘DXImageTransform.Microsoft.Alpha’].opacity;
} catch(e) {
try { // make sure its in the document
value = el.filters(‘alpha’).opacity;
} catch(err) {
}
}
return value / 100;
}
}
不得不说,这些事都是IE整出来的……










