如何修复IE6中背景图片的PNG透明度问题?
如何修复IE6中背景图片的PNG透明度问题?
我很喜欢David Cilley写的这个Javascript解决方案,它可以与任何后端一起使用,并且不会干扰兼容性良好的浏览器。但是,它仍然需要一个空白gif图像。
将这些函数添加到您的HTML头部或其他现有的.js文件中:
<script type="text/javascript">
function fixPngs(){
// Loops through all img tags
for (i = 0; i < document.images.length; i++){
var s = document.images[i].src;
if (s.indexOf('.png') > 0) // Checks for the .png extension
fixPng(s, document.images[i]);
}
}
function fixPng(u, o){
// u = url of the image
// o = image object
o.src = 'images/spacer.gif'; // Need to give it an image so we don't get the red x
o.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + u + "', sizingMethod='scale')";
}
</script>
<!--[if lte IE 6]>
<script language="javascript" type="text/javascript">
//this is a conditional comment that only IE understands. If a user using IE 6 or lower
//views this page, the following code will run. Otherwise, it will appear commented out.
//it goes after the body tag so it can fire after the page loads.
fixPngs();
</script>
<![endif]-->
如果想要更全面地解决IE6的问题,可以尝试使用IE7 Javascript库。
var oldw = o.clientWidth; var oldh = o.clientHeight;
,并在调用后恢复,使用if(o.style) { o.style.width = oldw +"px"; o.style.height = oldh +"px"; }
。 - user1092196使用PNG行为。
ie6.css:
img {
behavior: url("pngbehavior.htc");
}
page.html:
<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="ie6.css" />
<![endif]-->
bKGD
块,指定所需的背景颜色。只需输入你需要显示PNG的颜色,就完成了。你可以勇敢地声明你的网站在IE6上可能无法正常渲染。这可能不是最商业化的方法,但如果我们让IE6消失,我们所有人(甚至包括微软)都会受益。当然,由于大量在线活动发生在安装了IE6的公司机器上,这并不会很快发生。:(
1.在文档的头部块中为IE6添加条件性CSS:
<!--[if (IE 6)]>
<link rel="stylesheet" type="text/css" href="locationOfyourCss/ie6.css"/>
<![endif]-->
2.Assign class name in your element:
<td class = yourClassName>
3.In ie6.css, apply filter. You have to specify width and height. Set background image to clear.cache.gif, the filter doesn't work without those properties:
.yourClassName{
width:8px;
height:22px;
background: url("locationOfBlankImage/clear.cache.gif");
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='filePath/imageName.png', sizingMethod='scale');
}
<!--[if lt IE 7 ]> <body class="ie6 pageStyle"> <![endif]-->
<!--[if IE 7 ]> <body class="ie7 pageStyle"> <![endif]-->
<!--[if IE 8 ]> <body class="ie8 pageStyle"> <![endif]-->
<!--[if IE 9 ]> <body class="ie9 pageStyle"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <body class="pageStyle"> <!--<![endif]-->
.someStyle {
background: url(/images/someFile.png) no-repeat;
background-position: -0px -280px;
}
.ie6 .someStyle {
background: url(/images/someFile.gif) no-repeat;
background-position: -0px -280px;
}