您當(dāng)前位置:圖趣網(wǎng)(Tuquu) >> 網(wǎng)頁設(shè)計教程 >> ps教程 >> 瀏覽設(shè)計教程

網(wǎng)頁設(shè)計中PNG背景在不同瀏覽器下的應(yīng)用

網(wǎng)頁設(shè)計中經(jīng)常會用到png透明圖片用來實現(xiàn)各種精美的變化特效。關(guān)于PNG透明背景在ie6和FIREFOX瀏覽器下顯示異常。
這里談一下不同瀏覽器內(nèi),如何應(yīng)用PNG圖片作背景。

一、在IE6中使用PNG背景
IE6本身并不認(rèn)識PNG圖片的透明特性,雖然有讓IE6支持PNG透明背景的JS程序:

  1. function correctPNG()
  2. {
  3. var arVersion = navigator.appVersion.split("MSIE")
  4. var version = parseFloat(arVersion[1])
  5. if ((version >= 5.5) && (document.body.filters))
  6. {
  7. for(var j=0; j<document.images.length; j++)
  8. {
  9. var img = document.images[j]
  10. var imgName = img.src.toUpperCase()
  11. if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
  12. {
  13. var imgID = (img.id) ? "id='" + img.id + "' " : ""
  14. var imgClass = (img.className) ? "class='" + img.className + "' " : ""
  15. var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
  16. var imgStyle = "display:inline-block;" + img.style.cssText
  17. if (img.align == "left") imgStyle = "float:left;" + imgStyle
  18. if (img.align == "right") imgStyle = "float:right;" + imgStyle
  19. if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
  20. var strNewHTML = "<span " + imgID + imgClass + imgTitle
  21. + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
  22. + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
  23. + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
  24. img.outerHTML = strNewHTML
  25. j = j-1
  26. }
  27. }
  28. }   
  29. }
  30. window.attachEvent("onload", correctPNG);

但是如果需要實現(xiàn)的地方僅有一處,還是用CSS來實現(xiàn)效率更高一些。這里應(yīng)用的是IE5.5+的AlphaImageLoader濾鏡:

filter:
progid:DXImageTransform.Microsoft.AlphaImageLoader
(src='temp.png',sizingMethod='scale')

需要注意的是:AlphaImageLoader濾鏡會導(dǎo)致該區(qū)域的鏈接和按鈕無效,解決的辦法是為鏈接或按鈕添加:position: relative;使其相對浮動。另外AlphaImageLoader無法設(shè)置背景的重復(fù),所以對圖片的切圖精度會有較高要求。

二、在IE7、Opera和firefox中使用PNG背景
這幾款瀏覽器對PNG背景的支持很好,可以直接應(yīng)用。而實際應(yīng)用中,我們需要同時照顧IE6瀏覽器,所以需要給樣式表加上*html來做兼容處理。也就是給同一標(biāo)簽兩次背景。

.uicss_cn{background:transparent url(../images/temp.png) repeat-x bottom left;height:3px;position:absolute;width:100%; font-size:0px;}

  1. *html .uicss_cn{background:transparent;
  2. filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="../images/temp.png", sizingMethod="crop");}

注意:無意中發(fā)現(xiàn),自己插入的PNG背景兼容代碼無效。最后證實問題出在圖片文件上。使用fireworks生成的部分png圖片,需要導(dǎo)出為PSD格式,再從PS里另存為PNG文件,即可。”

[教程作者:admin]
免責(zé)聲明:本站文章系圖趣網(wǎng)整理發(fā)布,如需轉(zhuǎn)載,請注明出處,素材資料僅供個人學(xué)習(xí)與參考,請勿用于商業(yè)用途!
本文地址:http://irelandcustomcontracting.com/tutorial/ps1143.html
網(wǎng)頁設(shè)計技巧之png圖片優(yōu)化壓縮
CSS Sprites 圖片整合技術(shù)
圖趣網(wǎng)微信
建議反饋
×