PNG圖像格式介紹:
PNG是20世紀90年代中期開始開發的圖像文件存儲格式,其目的是企圖替代GIF和TIFF文件格式,同時增加一些GIF文件格式所不具備的特 性。流式 網絡圖形格式(Portable Network Graphic Format,PNG)名稱來源於非官方的“PNG’s Not GIF”,是一種位圖文件(bitmap file)存儲格式,讀成“ping”。PNG用來存儲灰度圖像時,灰度圖像的深度可多到16位,存儲彩色圖像時,彩色圖像的深度可多到48位,並且還可 存儲多到16位的α通道數據。
IE6下PNG背景透明的顯示問題
PNG格式比起GIF來表現色彩更豐富,特別是表現漸變以及背景透明的漸變要比GIF格式出色很多,目前,最新的瀏覽器基本上都支持PNG格式。但是IE6不支持PNG背景透明,會顯示一個灰色的框。
IE6下PNG背景透明的解決辦法
.pngImg { background:url(image.png); _background:url(image.gif);}
注意上文的_號,目前IE7,8以及Firefox瀏覽器等都不支持此CSS語法,只有IE6識別。因此,其他瀏覽器會調用PNG,而IE6剛調用GIF。
background:url(a.png) repeat-x 0 0; _background:none;
_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="a.png"
,sizingMethod="crop");
上面的原理是其他調用PNG,IE6,則先設背景沒有,然後調用濾鏡使之顯示PNG圖片。
缺陷:IE6下背景無法平鋪,這個問題很嚴重。同時在性能上也有小問題,頁面中次數不是很多的時候該辦法還是可行的。
AlphaImageLoader濾鏡會導致該區域的鏈接和按鈕無效,解決的辦法是爲鏈接或按鈕添加:position: relative;這樣條代碼,使其相對浮動。AlphaImageLoader無法設置背景的重複,所以對圖片的切圖精度會有很高的精確度要求。
解決IE下的鏈接無效可用最後面的方法:
三.利用JS解決html中的img(插入在網頁中的png圖像)png背景灰問題
頁面中插入一段js即可。原理同上,只是將img標籤用<span>標籤替換掉,並且通過濾鏡設置該<span>標籤的background。它會將所有插入的PNG都如此處理。
<!--[if IE 6]>
<script>
function correctPNG()
{
for(var i=0; i<document.images.length; i++)
{
var img = document.images[i];
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;
i = i-1;
}
}
}
window.attachEvent("onload", correctPNG);
</script>
<![endif]-->
四.調用iepngfix.htc解決IE6PNG背景灰及拉伸問題
此方法來自:http://www.twinhelix.com/css/iepngfix/ 此方法基於Winodws平臺,在Linux下不支持htc,沒有驗證過,但有網友發文證實。
以下片段添加至css文件
<div class="pngImg">PNG背景圖片</div> <img src="png圖片" class="pngImg" alt="">
詳細的應用方法這裏就不介紹啦。
在逼不得已且身不由己必須使用PNG的情況下,這種方法應該是比較優秀的,雖然不能完美的解決IE6的平鋪,但是至少是實現了拉伸,使得很多情況下可以代替平鋪來使用。當然效率的問題任然是存在
.div { FILTER: alpha(opacity=20); moz-opacity: 0.2; opacity: 0.2;}
測試IE6,IE7,IE8,FF2,FF3均通過。提示:IE6,IE7需設置一個寬度(100%也行),否則看不到效果。
六.DD_belatedPNG,解決IE6不支持PNG絕佳方案
整個互聯網上解決這個IE6的透明PNG的方案也是多不勝數,從使用IE特有的濾鏡或是e xpression,再到javascript+透明GIF替代.但是這些方法都有一個缺點,就是不支持CSS中backgrond-position與 background-repeat.
而我今天介紹DD_belatedPNG,只需要一個理由,就是它支持backgrond-position與background-repeat.這是其他js插件不具備的.同時DD_belatedPNG還支持a:hover屬性,以及<img>.
看Demo: http://www.ediyang.com/demo/DD_Png/
原理
這個js插件使用了微軟的VML語言進行繪製,而其他多數解決PNG問題的js插件用的是AlphaImageLoader濾鏡.
使用方法
1.在這裏下載DD_belatedPNG.js文件.
http://dillerdesign.com/experiment/DD_belatedPNG/#download
2.在網頁中引用,如下:
<!--[if IE 6]>
<script src="DD_belatedPNG.js" mce_src="DD_belatedPNG.js"></script>
<script type="text/javascript"> /* EXAMPLE */
DD_belatedPNG.fix('.png_bg'); /* 將 .png_bg
改成你應用了透明PNG的CSS選擇器,例如我例子中的'.trans'*/ </script>
<![endif]-->
3.有2種調用函數,一種是DD_belatedPNG.fix(),如上代碼.另一種是fix(),這中方法需要在函數內指出css選擇器名.
使用a:hover請留意
5-25 更新:如果你也像jutoy同學一樣想要用透明PNG作爲a:hover時的背景圖片,那麼你需要留意你的代碼,需要以”a:hover”來作爲選擇器. 否則可能會導致無法成功.同時我也更新了demo,請需要的更新查看.接着我們看看正確的代碼:
<!--[if IE 6]>
<script type="text/javascript" src="js/DD_belatedPNG.js" ></script>
<script type="text/javascript"> DD_belatedPNG.fix('.trans,.box a:hover'); </script>
<![endif]-->
七.通過 javascript 和 css 濾鏡解決 IE 整站 png 背景透明問題
<script type="text/javascript" language="javascript">
function enablePngImages()
{ var imgArr = document.getElementsByTagName("IMG");
for(i=0; i<imgArr.length; i++){
if(imgArr[i].src.toLowerCase().lastIndexOf(".png") != -1)
{
imgArr[i].style.filter =
"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" +
imgArr[i].src + "', sizingMethod='auto')";
imgArr[i].src = "spacer.gif";
}
if(imgArr[i].currentStyle.backgroundImage.lastIndexOf(".png") != -1)
{
var img = imgArr[i].currentStyle.backgroundImage.substring(5,imgArr[i].currentStyle.backgroundImage.length-2);
imgArr[i].style.filter =
"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"',
sizingMethod='crop')";
imgArr[i].style.backgroundImage = "url(spacer.gif)";
}
}
}
function enableBgPngImages(bgElements)
{
for(i=0; i<bgElements.length; i++)
{
if(bgElements[i].currentStyle.backgroundImage.lastIndexOf(".png") != -1)
{ //alert(bgElements[i]);
var img =
bgElements[i].currentStyle.backgroundImage.substring(5,bgElements[i].currentStyle.backgroundImage.length-2);
bgElements[i].style.filter =
"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"',
sizingMethod='crop')";
bgElements[i].style.backgroundImage = "url(spacer.gif)";
}
}
}
</script>
<img src="pngpic.png" alt="" border="0" />
<!--[if lt IE 7]>
<script type='text/javascript'>
var bgElements; enablePngImages(); if(bgElements){ enableBgPngImages(bgElements); }
</script>
<![endif]-->
.pngImg {behavior: url(iepngfix.htc);}
以下片段添加至html文件一.IE6使用gif,其他則使用png來解決PNG背景灰