css实现html透明效果
2009-10-16 15:42
691 查看
CSS3草案中定义了A级浏览器所支持的用CSS实现元素透明的方案:
实际上在IE8中,-ms-filter是filter的别名,两者区别是-ms-filter的属相值必须被单引号或双引号包围,而filter中则不是必须,而在IE8之前的版本中,filter的属性值必须不被单引号或双引号包围。
IE中的HTML元素要实现透明,则其必须具备layout,这样的元素有仅可读的属性hasLayout,且其值为true。具体情况如下:
设置
设置
设置
设置
IE8兼容模式和IE8以前的浏览器中,在标准(strict)模式下其demo3就不行。
元素在怪癖模式(compatmode)下。
设置了
设置了
元素的
在IE8标准模式下设置了demo8。
关于hasLayout的更多详情可以看ExploringInternetExplorer“HasLayout”Overview和Onhavinglayout
从上面就可以看出IE实现HTML元素的透明如此混乱,为了向下兼容和自己的私有属性让IE上实现元素透明有多种方式,比如CSSOpacity实例中的demo1到demo8,虽然IE团队推荐实现透明的方式是:
而目前简单最好用的实现方式是如CSSOpacity中demo4这样:
实际上目前最流行的JavaScript框架的设置样式方法都是应用这种方式,并且针对IE设置了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:
{opacity:|inherit;}来声明元素的透明度,这已经得到了大多数现代浏览器的支持,而IE则很早通过特定的私有属性
filter来实现的,所以HTML元素的透明效果已经无处不在了。首先看看
浏览器 | 最低 版本 | 方案 |
---|---|---|
InternetExplorer | 4.0 | filter:alpha(opacity=xx); |
5.5 | filter:progid:DXImageTransform.Microsoft.Alpha(opacity=xx); | |
8.0 | filter:"alpha(opacity=xx)"; filter:"progid:DXImageTransform.Microsoft.Alpha(opacity=xx)"; -ms-filter:"alpha(opacity=xx)"; -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(opacity=30)"; | |
Firefox(Gecko) | 0.9(1.7) | opacity |
Opera | 9.0 | opacity |
Safari(WebKit) | 1.2(125) | opacity |
IE中的HTML元素要实现透明,则其必须具备layout,这样的元素有仅可读的属性
body、
img、
table、
tr、
th、
td等元素的
hasLayout一直为
true。
type为
text、
button、
file或
select的
input的
hasLayout一直为
true。
设置
{position:absolute}的元素的
hasLayout为
true
设置
{float:left|right}的元素的
hasLayout为
true
设置
{display:inline-block}的元素的
hasLayout为
true
设置
{height:xx}或
{width:xx}的元素必须具体以下两个条件之一,其
hasLayout才能为
true:
IE8兼容模式和IE8以前的浏览器中,在标准(strict)模式下其
display的值是
block,如
元素在怪癖模式(compatmode)下。
设置了
{zoom:xx}的元素在IE8的兼容模式或IE8之前的浏览器中其
hasLayout为
true,但在IE8的标准模式下则不会触发
hasLayout。
设置了
{writing-mode:tb-rl}的元素的
hasLayout为
true。
元素的
contentEditable的属性值为
true。
在IE8标准模式下设置了
{display:block}的元素的
hasLayout一直为
true,如
关于hasLayout的更多详情可以看
从上面就可以看出IE实现HTML元素的透明如此混乱,为了向下兼容和自己的私有属性让IE上实现元素透明有多种方式,比如
{ -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"; filter:progid:DXImageTransform.Microsoft.Alpha(Opacity=50); opacity:.5; }
而目前简单最好用的实现方式是
{ filter:alpha(opacity=30); opacity:.3; }
实际上目前最流行的JavaScript框架的设置样式方法都是应用这种方式,并且针对IE设置了
{zoom:1}来让元素的
hasLayout为
true,但在IE8的标准模式下
zoom并不能触发
hasLayout,所以利用它们设置
hasLayout为
false的元素的透明度时在IE8的标准模式下是失败的,这个bug在
我简单的实现了设置和获取Opacity的函数,可以避开上面框架存在的bug,请
//设置CSSopacity属性的函数,解决IE8的问题 varsetOpacity=function(el,i){ if(window.getComputedStyle){//fornon-IE el.style.opacity=i; }elseif(document.documentElement.currentStyle){//forIE 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+')'; } } } //获取CSSopacity属性值的函数 //借鉴自http://developer.yahoel.com/yui/docs/YAHOO.util.Dom.html#method_getStyle vargetOpacity=function(el){ varvalue; if(window.getComputedStyle){ value=el.style.opacity; if(!value){ value=el.ownerDocument.defaultView.getComputedStyle(el,null)['opacity']; } returnvalue; }elseif(document.documentElement.currentStyle){ value=100; try{//willerrorifnoDXImageTransform value=el.filters['DXImageTransform.Microsoft.Alpha'].opacity; }catch(e){ try{//makesureitsinthedocument value=el.filters('alpha').opacity; }catch(err){ } } returnvalue/100; } }
附:弹出注册框实例
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf8"/> <title>弹出提示</title> <mce:style><!-- *{margin:0;padding:0;font-size:12px;} html,body{height:100%;width:100%;} #content{background:#f8f8f8;padding:30px;height:100%;} #contenta{font-size:30px;color:#369;font-weight:700;} #alert{border:1pxsolid#369;width:300px;height:150px;background:#e2ecf5;z-index:1000;position:absolute;display:none;} #alerth4{height:20px;background:#369;color:#fff;padding:5px005px;} #alerth4span{float:left;} #alerth4span#close{margin-left:210px;font-weight:500;cursor:pointer;} #alertp{padding:12px0030px;} #alertpinput{width:120px;margin-left:20px;} #alertpinput.myinp{border:1pxsolid#ccc;height:16px;} #alertpinput.sub{width:60px;margin-left:30px;} --></mce:style><stylemce_bogus="1"> *{margin:0;padding:0;font-size:12px;} html,body{height:100%;width:100%;} #content{background:#f8f8f8;padding:30px;height:100%;} #contenta{font-size:30px;color:#369;font-weight:700;} #alert{border:1pxsolid#369;width:300px;height:150px;background:#e2ecf5;z-index:1000;position:absolute;display:none;} #alerth4{height:20px;background:#369;color:#fff;padding:5px005px;} #alerth4span{float:left;} #alerth4span#close{margin-left:210px;font-weight:500;cursor:pointer;} #alertp{padding:12px0030px;} #alertpinput{width:120px;margin-left:20px;} #alertpinput.myinp{border:1pxsolid#ccc;height:16px;} #alertpinput.sub{width:60px;margin-left:30px;}</style> </head> <body> aa <divid="content"> <ahref="#"mce_href="#"style="position:absolute;top:50%;left:50%">注册</a> </div> <divid="alert"> <h4><span>现在注册</span><spanid="close">关闭</span></h4> <p><label>用户名</label><inputtype="text"class="myinp"onmouseover="this.style.border='1pxsolid#f60'"onfoucs="this.style.border='1pxsolid#f60'"onblur="this.style.border='1pxsolid#ccc'"/></p> <p><label>密 码</label><inputtype="password"class="myinp"onmouseover="this.style.border='1pxsolid#f60'"onfoucs="this.style.border='1pxsolid#f60'"onblur="this.style.border='1pxsolid#ccc'"/></p> <p><inputtype="submit"value="注册"class="sub"/><inputtype="reset"value="重置"class="sub"/></p> </div> <mce:scripttype="text/javascript"><!-- varmyAlert=document.getElementById("alert"); varreg=document.getElementById("content").getElementsByTagName("a")[0]; varmClose=document.getElementById("close"); reg.onclick=function() { myAlert.style.display="block"; myAlert.style.position="absolute"; myAlert.style.top="50%"; myAlert.style.left="50%"; myAlert.style.marginTop="-75px"; myAlert.style.marginLeft="-150px"; mybg=document.createElement("div"); mybg.setAttribute("id","mybg"); mybg.style.background="#E0ECF8"; mybg.style.width="100%"; mybg.style.height="100%"; mybg.style.position="absolute"; mybg.style.top="0"; mybg.style.left="0"; mybg.style.zIndex="500"; mybg.style.opacity="0.3"; mybg.style.filter="Alpha(opacity=30)"; document.body.appendChild(mybg); document.body.style.overflow="hidden"; } mClose.onclick=function() { myAlert.style.display="none"; mybg.style.display="none"; } //--></mce:script> </body> </html>
相关文章推荐
- html css实现左右两列固定宽度中间自适应效果
- JS+CSS实现弹出全屏灰黑色透明遮罩效果的方法
- CSS学习笔记-附加篇( CSS+JS实现的选项卡效果(html组件))
- 用CSS实现像vista一样很酷的透明效果(附漂亮截图)
- css 实现透明效果 。。支持ie,ff。
- HTML+CSS----IE6下背景透明实现-css/js--png图片
- [置顶] css实现HTML文件上传的美化效果
- css技巧:实现圆角、阴影、透明效果
- HTML开发——项目心得(CSS实现导航栏选项卡炫酷渐变效果以及图片替换文本显示)
- atitit.按钮光标滑过高亮切换以及其他动态效果的实现css html js --attilax总结
- 用原生js+HTML+CSS实现一个弹幕的效果
- 纯CSS实现背景半透明文字不透明效果兼容IE6
- JS+CSS实现弹出全屏灰黑色透明遮罩效果的方法
- html+css源码之实现登录弹出框遮罩层效果
- 用css实现透明效果
- 【Html码农】CSS实现背景透明,文字不透明,兼容所有浏览器
- 使用html+js+css 实现页面轮播图效果(实例讲解)
- text-decoration <del> <s> 实现横线从文字中间穿过的CSS效果属性和html标签
- css+filter实现简单的图片透明效果
- CSS实现Div层背景半透明而内容不透明的效果