2019独角兽企业重金招聘Python工程师标准>>>
做Web开发的朋友一定都知道PNG是一个相当不错的图片格式,但是这个好的格式却在IE6时代造成了麻烦,IE6会使透明的PNG的透明部分出现#DBEAED的色彩。透明不了。使得在FF下开发表现很好的界面换成IE浏览就惨不忍睹,又逼着换成GIF,而GIF的假透明在变换背景时造成毛边现象。
图片在网页中有两种,一是直接的标签,还有是放在css引用中作背景,这两种方式的PNG透明处理方法也不一样。
首先看标签的解决办法。这号称是官方的解决方案,下面是核心函数:
Code
1 /*
2 Correctly handle PNG transparency in Win IE 5.5 & 6.
3 Copyright 2007 Ignia, LLC
4 Based in part on code from from http://homepage.ntlworld.com/bobosola.
5
6 Use in with DEFER keyword wrapped in conditional comments:
7
8
9
10 */
11
12 function fixPng() {
13 var arVersion = navigator.appVersion.split("MSIE")
14 var version = parseFloat(arVersion[1])
15
16 if ((version >&#61; 5.5 && version < 7.0) && (document.body.filters)) {
17 for(var i&#61;0; i<document.images.length;></document.images.length;> var img &#61; document.images[i];
18 var imgName &#61; img.src.toUpperCase();
19 if (imgName.indexOf(".PNG") > 0) {
20 var width &#61; img.width;
21 var height &#61; img.height;
22 var sizingMethod &#61; (img.className.toLowerCase().indexOf("scale") >&#61; 0)? "scale" : "image";
23 img.runtimeStyle.filter &#61; "progid:DXImageTransform.Microsoft.AlphaImageLoader(src&#61;&#39;" &#43; img.src.replace(&#39;%23&#39;, &#39;%2523&#39;).replace("&#39;", "%27") &#43; "&#39;, sizingMethod&#61;&#39;" &#43; sizingMethod &#43; "&#39;)";
24 img.src&#61;"images/blank.gif" mce_src&#61;"images/blank.gif";
25 img.width &#61; width;
26 img.height &#61; height;
27 }
28 }
29 }
30 }
31
32 fixPng();
1 /*
2 Correctly handle PNG transparency in Win IE 5.5 & 6.
3 Copyright 2007 Ignia, LLC
4 Based in part on code from from http://homepage.ntlworld.com/bobosola.
5
6 Use in with DEFER keyword wrapped in conditional comments:
7
8
9
10 */
11
12 function fixPng() {
13 var arVersion &#61; navigator.appVersion.split("MSIE")
14 var version &#61; parseFloat(arVersion[1])
15
16 if ((version >&#61; 5.5 && version < 7.0) && (document.body.filters)) {
17 for(var i&#61;0; i<document.images.length;></document.images.length;> var img &#61; document.images[i];
18 var imgName &#61; img.src.toUpperCase();
19 if (imgName.indexOf(".PNG") > 0) {
20 var width &#61; img.width;
21 var height &#61; img.height;
22 var sizingMethod &#61; (img.className.toLowerCase().indexOf("scale") >&#61; 0)? "scale" : "image";
23 img.runtimeStyle.filter &#61; "progid:DXImageTransform.Microsoft.AlphaImageLoader(src&#61;&#39;" &#43; img.src.replace(&#39;%23&#39;, &#39;%2523&#39;).replace("&#39;", "%27") &#43; "&#39;, sizingMethod&#61;&#39;" &#43; sizingMethod &#43; "&#39;)";
24 img.src&#61;"images/blank.gif" mce_src&#61;"images/blank.gif";
25 img.width &#61; width;
26 img.height &#61; height;
27 }
28 }
29 }
30 }
31
32 fixPng();
再看css背景方式的解决办法&#xff1a;
这个方法是Dean Edwards 的 IE7提出的。英语还过的去的可以直接去看原文&#xff1a;http://dean.edwards.name/ http://code.google.com/p/ie7-js/
首先要下载这个js&#xff0c;然后在加入下面这段
再把PNG 图片名字存为“你的文件名-trans.png”。文件名字记得一定要有 -trans。例如&#xff1a; img-trans.png&#xff0c; tupian-trans.png&#xff0c; logo-trans.png
ps&#xff1a;这个方法不支持IE5&#xff0c;但在IE5.5和IE6下正常。