网站中的PNG图片透明问题

来源:互联网 发布:淘宝店铺关键词设置 编辑:程序博客网 时间:2024/05/16 07:59
最近由于用户的电脑上大多安装的IE6,并且都没有更新至IE7或8.
FF对于大部分普通网民使用的则更少。
那么网站中的PNG图片透明问题在IE6就显得很严重了。
所以找了一些高手的资料后就记录在这里了:希望大家有用的 多多分享。


先说CCS方法:对于有背景图片的容器写个CSS。布局的不说,然后在里面加个

_filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='scale', src="/templets/images/news_bg.png");_background:none;}

示例:

#div1{

clear:both;

float:left;

width:364px;

height:212px;

background:url(xxx.png) top no-repeat;

_filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', sizingMethod='scale', src="xxx.png");

_background:none;}

}

也就是多了这两句:两个语句中的一些属性的值有 enabled : 可选项。布尔值(Boolean)。
设置或检索滤镜是否激活。true | false,

true : 默认值。滤镜激活。  

false : 滤镜被禁止。

 


sizingMethod : 可选项。字符串(String)。设置或检索滤镜作用的对象的图片在对象容器边界内的显示方式。
crop : 剪切图片以适应对象尺寸。  

image : 默认值。增大或减小对象的尺寸边界以适应图片的尺寸。  

scale : 缩放图片以适应对象的尺寸边界。

 


src : 必选项。字符串(String)。使用绝对或相对 url 地址指定背景图像。
假如忽略此参数,滤镜将不会作用.

 


这样效果可以实现,也不增加多少代码量。

不过需要注意的是:AlphaImageLoader滤镜会导致该区域的链接和按钮无效,
解决的办法是为链接或按钮使用相对定位,或者添加:position: relative;这样条代码,使其相对浮动。

最后AlphaImageLoader无法设置背景的重复,所以对图片的切图精度会有很高的精确度要求。

 

 

 

然后是JS的方法。比较推荐:

这种方法在<head></head>中写入这段JS代码就可以让网页中所以直接放置的PNG图片背景透明。

<script language="JavaScript" type="text/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>

非常方便,有需要的朋友可以拿去试试。美工-小接编