Home > Article > Web Front-end > Summary of solutions to why png cannot be transparent under IE6_javascript techniques
FF and IE7 already directly support transparent PNG images. The following is mainly used to solve the problem of gray background in transparent PNG images under IE6
Syntax:
filter : progid:DXImageTransform.Microsoft.AlphaImageLoader ( enabled=bEnabled , sizingMethod=sSize , src=sURL )
enabled : Optional. Boolean. Sets or retrieves whether the filter is active. true | false true : Default value. Filter activated.
false : filter is disabled.
sizingMethod : Optional. String. Sets or retrieves how the image of the filtered object is displayed within the boundaries of the object container. crop : Crops the image to fit the object dimensions.
image: Default value. Increase or decrease the object's size bounds to fit the dimensions of the picture.
scale : Scale the image to fit within the object's size boundaries.
src: required. String. Specify the background image using an absolute or relative url address. If this parameter is omitted, the filter will have no effect.
Features:
Enabled: Readable and writable. Boolean. See enabled attribute.
sizingMethod: Readable and writable. String. See the sizingMethod property.
src: readable and writable. String. See the src attribute.
Description:
Displays an image between the object's background and content, within the bounds of the object's container. And provide operations for cutting and resizing this image. If the PNG (Portable Network Graphics) format is loaded, 0%-100% transparency is also provided.
The transparency of images in PNG (Portable Network Graphics) format does not prevent you from selecting text. In other words, you can choose to display the content behind the completely transparent area of the image in PNG (Portable Network Graphics) format.
Example: Solve the problem of png transparency failure under IE6.
CSS style:
.png{ _background: url(images/angel.png) no-repeat !important; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=noscale, src="images/angel.png"); background:none; width:118px;height:133px; } .png div{position:relative;}
HTML code:
<div class="png"> <div> CSS 背景PNG透明 及 链接失效问题解决 </div> </div>
/*
Compatible with IE6.0, IE7.0, FF,
IE7.0 and the new version of FF can actually make it transparent without adding a filter.
*/
================================================== ==============================
Method 1: Define a style. After applying this style to a certain div, the transparent png background image of the div will automatically become transparent. (Note that the paths of the two images are written differently. In this example, the icon_home.png image and the html file are in the same directory)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <style type="text/css"> <!-- .qq { height: 90px; width: 90px; background-image: url(icon_home.png)!important;/* FF IE7 */ background-repeat: no-repeat; _filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='icon_home.png'); /* IE6 */ _ background-image: none; /* IE6 */ } --> </style> </head> <body> <div class="qq"></div> </body> </html>
Method 2: Define the style for img, and all transparent PNGs on the page will be automatically transparent. (This method is only valid for directly inserted pictures, not for background pictures) Note that you need to prepare a small transparent picture transparent.gif, the size is not limited. Must be placed in the same directory as html
Please do not use it in large quantities, otherwise it will cause the page to open very slowly!!!)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <style type="text/css"> .mypng img { azimuth: expression( this.pngSet?this.pngSet=true:(this.nodeName == "IMG" && this.src.toLowerCase().indexOf('.png')>-1?(this.runtimeStyle.backgroundImage = "none", this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "', sizingMethod='image')", this.src = "transparent.gif"):(this.origBg = this.origBg? this.origBg :this.currentStyle.backgroundImage.toString().replace('url("','').replace('")',''), this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.origBg + "', sizingMethod='crop')", this.runtimeStyle.backgroundImage = "none")),this.pngSet=true); } </style> </head> <body> 换成你的png图片 <div class="mypng"> <img src="icon_face_07.png" width="30" height="30" /> <img src="icon_face_10.png" width="30" height="30" /> <img src="icon_face_08.png" width="30" height="30" /> </div> </body> </html>
Method 3: Use JS to implement it. After adding a piece of js code, all inserted transparent pngs will automatically become transparent. (Note that this method is only valid for directly inserted images, not for background images. )
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <script language="JavaScript"> function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6. { var arVersion = navigator.appVersion.split("MSIE") var version = parseFloat(arVersion[1]) if ((version >= 5.5) && (document.body.filters)) { for(var j=0; j<document.images.length; j++) { var img = document.images[j] var imgName = img.src.toUpperCase() if (imgName.substring(imgName.length-3, imgName.length) == "PNG") { var imgID = (img.id) ? "id='" + img.id + "' " : "" var imgClass = (img.className) ? "class='" + img.className + "' " : "" var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' " var imgStyle = "display:inline-block;" + img.style.cssText if (img.align == "left") imgStyle = "float:left;" + imgStyle if (img.align == "right") imgStyle = "float:right;" + imgStyle if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" img.outerHTML = strNewHTML j = j-1 } } } } window.attachEvent("onload", correctPNG); </script> <style type="text/css"> <!-- body { } --> </style></head> <body> 把图片换成你自己的图片 <img src="img/icon_face_03.png" width="30" height="30" /><!--把图片换成你自己的图片 --> <img src="img/icon_face_05.png" width="30" height="30" /> <img src="img/menu_title_over.png" width="130" height="36" /> </body> </html>
Method 4
<script language="javascript"> // 修复 IE 下 PNG 图片不能透明显示的问题 function fixPNG(myImage) { var arVersion = navigator.appVersion.split("MSIE"); var version = parseFloat(arVersion[1]); if ((version >= 5.5) && (version < 7) && (document.body.filters)) { var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""; var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : ""; var imgTitle = (myImage.title) ? "title='" + myImage.title + "' " : "title='" + myImage.alt + "' "; var imgStyle = "display:inline-block;" + myImage.style.cssText; var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + myImage.width + "px; height:" + myImage.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>"; myImage.outerHTML = strNewHTML; } } window.onload=function(){ document.getElementById("top").style.height=screen.height/5+"px"; }// </script>
Usage is as follows:
The above is the entire content of this article, I hope you all like it.