轻松解决PNG图片在IE6中背景不透明方法
2012-02-27 09:20
716 查看
最近在读javascript DOM高级程序设计,里面中有一个更好的代码,这种方式基本满足所有的PNG嵌入需求,代码如下:
把这个代码存为一个JS文件,只需在页面上引用此JS即可
解决一下PNG的引用方式:
1、以PNG作为源文件的<img>元素
2、使用<LINK>元素或者@import规则导入的样式表中,使用透明PNG图像作为元素背景
3、在嵌入式的样式属性中将透明的PNG图像定义为元素的背景
按如下方式应用此JS即可:
此方法存在一个BUG,但是出现的几率很小,如下:
function fixMSIEPng() { if (!document.body.filters) { // Not MSIE return; } if (7 <= parseFloat(navigator.appVersion.split("MSIE")[1])) { // 7+ supports PNG return; } // Fix the inline images if (document.images) { var images = document.images; var img = null; for (var i = images.length - 1; img = images[i]; i--) { // Check if it's PNG image if (img.src && img.src.substring( img.src.length - 3, img.src.length).toLowerCase() !== 'png') { // Skip it continue; } // Build the style property for the outer element var inlineStyle = ''; if (img.align == 'left' || img.align == 'right') { inlineStyle += 'float:' + img.align + ';'; } if (img.parentElement.nodeName == 'A') { // This image is inside an anchor so show a hand inlineStyle += 'cursor:hand;'; } // Make the display inline-block so that it can have a width // and height yet still be positioned properly inlineStyle += 'display:inline-block;'; // Grab any other CSS style applied to the element if (img.style && img.style.cssText) { inlineStyle += img.style.cssText; } // Wrap a <span> around the image with the appropriate style // and information such as className and ID img.outerHTML = '<span ' + (img.id ? ' id="' + img.id + '"' : '') + (img.className ? 'class="' + img.className + '" ' : '') + ' style="width:' + img.width + 'px; height:' + img.height + 'px;' + inlineStyle + ';filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + img.src + '\', sizingMethod=\'scale\');"></span>'; } } // Create an private method to apply in the next set of loops // This sets the appropriate styles for the elements function addFilters(e) { // Check if the element has style, a background and verify // it doesn't already have a filter applied if ( e.style && e.style.background && !e.style.filter) { // Check if it's a PNG var src = null; if (src = e.style.backgroundImage.match(/^url\((.*\.png)\)$/i)) { e.style.backgroundColor = 'transparent'; e.style.backgroundImage = 'url()'; e.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src[1] + "',sizingMethod='" + ((e.style.width && e.style.height) ? 'scale' : 'crop') + "')"; } } } // Create a private recursive processing method to apply the // addFilters() method to the style sheets function processRules(styleSheet) { for (var i in styleSheet.rules) { addFilters(styleSheet.rules[i]); } //recurse for @import stylesheets... if (styleSheet.imports) { for (var j in styleSheet.imports) { processRules(styleSheet.imports[j]); } } } // Process each style sheet var styleSheets = document.styleSheets; for (var i = 0; i < styleSheets.length; i++) { processRules(styleSheets[i]); } // Fix the inline style properties if (document.all) { var all = document.all; for (var i = 0; i < all.length; i++) { addFilters(all[i]); } } } if (window.attachEvent) window.attachEvent("onload", fixMSIEPng);
把这个代码存为一个JS文件,只需在页面上引用此JS即可
解决一下PNG的引用方式:
1、以PNG作为源文件的<img>元素
2、使用<LINK>元素或者@import规则导入的样式表中,使用透明PNG图像作为元素背景
3、在嵌入式的样式属性中将透明的PNG图像定义为元素的背景
按如下方式应用此JS即可:
<!--[if IE 6]> <script src="images/IEpng.js" language="javascript" type="text/javascript"></script> <![endif]-->
此方法存在一个BUG,但是出现的几率很小,如下:
相关文章推荐
- 解决PNG图片在IE6中背景不透明方法
- IE6下png图片和背景图片透明问题解决方法
- 解决png图片在IE6背景无法透明不兼容解决方法
- 解决PNG图片在IE6中背景不透明方法_解决IE6中PNG背景不透明方法
- IE6下PNG透明图片背景显示灰色的完美解决方法
- 解决IE6中PNG背景不透明方法 解决PNG图片在IE6中背景不透明方法
- ie6 png 图片背景透明解决方法
- IE6显示PNG图片背景透明的解决方法
- 使用DD_belatedPNG让IE6支持PNG透明图片 及IE6下背景图闪烁问题的解决方法
- 基于jQuery IE6下PNG图片背景透明问题解决方法
- 解决IE6下PNG图片背景不透明的三种方法(CSS/JS/JQuery)
- 完美解决PNG图片在IE6中背景不透明的方法
- 解决PNG图片在IE6中背景不透明的CSS与JS的代码
- 解决PNG图片在IE6中背景不透明的CSS与JS代码
- 新分享一种解决ie6下PNG图片透明的方法
- 使IE6下PNG背景图片透明的七种方法
- ie6下png图片背景不透明的解决办法使用js实现
- 【转】CSS解决IE6下PNG图片背景不透明的问题
- IE6中PNG透明背景图片处理方法