ie6实现png图片透明显示的方法

ie6 实现PNG图片透明的方法:
一、滤镜 (实现png背景图片透明显示)
1、书写正常的CSS代码,通过background导入图片,这样所有的浏览器均使用了此PNG图片;
background:url(../images/W3CfunsLogo.png);  但是ie6下png图片透明的地方会有浅色背景。

2、通过滤镜引入图片,滤镜引入图片的时候是相对于HTML文件,而不是相对于CSS文件,语法如下:
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/W3CfunsLogo.png");

如:

#pics
{
background:url(../images/W3CfunsLogo.png) no-repeat;

/*以下为IE6设置PNG透明代码*/
_background:none;
_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/W3CfunsLogo.png");
}

方案2 - HTC插件解决方案:

介绍:从IE 5.5版本开始,Internet Explorer(IE)开始支持Web 行为的概念。这些行为是由后缀名为.htc的脚本文件描述的,它们定义了一套方法和属性,程序员几乎可以把这些方法和属性应用到HTML页面上的任何元素上去。

iepngfix.htc:

<public:component>
<script type="text/javascript">

// IE5.5+ PNG Alpha Fix v2.0 Alpha
// (c) 2004-2008 Angus Turnbull http://www.twinhelix.com

// This is licensed under the GNU LGPL, version 2.1 or later.
// For details, see: http://creativecommons.org/licenses/LGPL/2.1/

var IEPNGFix = window.IEPNGFix || {};
IEPNGFix.data = IEPNGFix.data || {};


// This must be a path to a blank image, relative to the HTML document(s).
// In production use I suggest '/images/blank.gif' or similar. That's all!
IEPNGFix.blankImg = 'images/blank.gif';


IEPNGFix.fix = function(elm, src, t) {
    // Applies an image 'src' to an element 'elm' using the DirectX filter.
    // If 'src' is null, filter is disabled.
    // Disables the 'hook' to prevent infinite recursion on setting BG/src.
    // 't' = type, where background tile = 0, background = 1, IMG SRC = 2.

    var h = this.hook.enabled;
    this.hook.enabled = 0;

    var f = 'DXImageTransform.Microsoft.AlphaImageLoader';
        src = (src || '').replace(/\(/g, '%28').replace(/\)/g, '%29');

    if (
        src && !(/IMG|INPUT/.test(elm.nodeName) && (t != 2)) &&
        elm.currentStyle.width == 'auto' && elm.currentStyle.height == 'auto'
    ) {
        elm.style.width = elm.offsetWidth + 'px';
        elm.style.height = elm.clientHeight + 'px';
        if (elm.currentStyle.display == 'inline') {
            elm.style.display = 'inline-block';
        }
    }

    if (t == 1) {
        elm.style.backgroundImage = 'url("' + this.blankImg + '")';
    }
    if (t == 2) {
        elm.src = this.blankImg;
    }

    if (elm.filters[f]) {
        elm.filters[f].enabled = src ? true : false;
        if (src) {
            elm.filters[f].src = src;
        }
    } else if (src) {
        elm.style.filter = 'progid:' + f + '(src="' + src +
            '",sizingMethod="' + (t == 2 ? 'scale' : 'crop') + '")';
    }

    this.hook.enabled = h;
};


IEPNGFix.process = function(elm, init) {
    // Checks the onpropertychange event (on first 'init' run, a fake event)
    // and calls the filter-applying-functions.

    if (
        !/MSIE (5\.5|6)/.test(navigator.userAgent) ||
        typeof elm.filters == 'unknown'
    ) {
        return;
    }
    if (!this.data[elm.uniqueID]) {
        this.data[elm.uniqueID] = {
            className: ''
        };
    }
    var data = this.data[elm.uniqueID],
        evt = init ? { propertyName: 'src,backgroundImage' } : event,
        isSrc = /src/.test(evt.propertyName),
        isBg = /backgroundImage/.test(evt.propertyName),
        isPos = /width|height|background(Pos|Rep)/.test(evt.propertyName),
        isClass = !init && ((elm.className != data.className) &&
            (elm.className || data.className));
    if (!(isSrc || isBg || isPos || isClass)) {
        return;
    }
    data.className = elm.className;
    var blank = this.blankImg.match(/([^\/]+)$/)[1],
        eS = elm.style,
        eCS = elm.currentStyle;

    // Required for Whatever:hover - erase set BG if className changes.
    if (
        isClass && (eS.backgroundImage.indexOf('url(') == -1 ||
        eS.backgroundImage.indexOf(blank) > -1)
    ) {
        return setTimeout(function() {
            eS.backgroundImage = '';
        }, 0);
    }

    // Foregrounds.
    if (isSrc && elm.src &&  { IMG: 1, INPUT: 1 }[elm.nodeName]) {
        if ((/\.png/i).test(elm.src)) {
            this.fix(elm, elm.src, 2);
        } else if (elm.src.indexOf(blank) == -1) {
            this.fix(elm, '');
        }
    }

    // Backgrounds.
    var bgSrc = eCS.backgroundImage || eS.backgroundImage;
    if ((bgSrc + elm.src).indexOf(blank) == -1) {
        var bgPNG = bgSrc.match(/url[("']+(.*\.png[^\)"']*)[\)"']/i);
        if (bgPNG) {
            if (this.tileBG && !{ IMG: 1, INPUT: 1 }[elm.nodeName]) {
                this.tileBG(elm, bgPNG[1]);
                this.fix(elm, '', 1);
            } else {
                if (data.tiles && data.tiles.src) {
                    this.tileBG(elm, '');
                }
                this.fix(elm, bgPNG[1], 1);
                this.childFix(elm);
            }
        } else {
            if (data.tiles && data.tiles.src) {
                this.tileBG(elm, '');
            }
            this.fix(elm, '');
        }
    } else if ((isPos || isClass) && data.tiles && data.tiles.src) {
        this.tileBG(elm, data.tiles.src);
    }

    if (init) {
        this.hook.enabled = 1;
        elm.attachEvent('onpropertychange', this.hook);
    }
};


IEPNGFix.childFix = function(elm) {
    // "hasLayout" fix for unclickable children inside PNG backgrounds.
    var tags = [
            'a',
            'input',
            'select',
            'textarea',
            'button',
            'iframe',
            'object'
        ],
        t = tags.length,
        tFix = [];
    while (t--) {
        var pFix = elm.all.tags(tags[t]),
            e = pFix.length;
        while (e--) {
            tFix.push(pFix[e]);
        }
    }
    t = tFix.length;
    if (t && (/relative|absolute/i).test(elm.currentStyle.position)) {
        alert('IEPNGFix: Unclickable children of element:' +
            '\n\n<' + elm.nodeName + (elm.id && ' id=' + elm.id) + '>');
    }
    while (t--) {
        if (!(/relative|absolute/i).test(tFix[t].currentStyle.position)) {
            tFix[t].style.position = 'relative';
        }
    }
};


IEPNGFix.hook = function() {
    if (IEPNGFix.hook.enabled) {
        IEPNGFix.process(element, 0);
    }
};


IEPNGFix.process(element, 1);

</script>
</public:component>

 

~~~在样式中为加载png图像的img或png背景图的元素,定义 behavior:url(iepngfix.htc);  //htc路径是相对于html的,用绝对路径也可以

~~~原理上来讲其实是用alphaImageLoader滤镜来实现png的透明,原来的背景图background-img或src用一张透明的gif替代
<style type="text/css">
img,div{behavior:url(style/iepngfix.htc);}
</style>

 

方案3 - 纯CSS解决方案:(~~img标签的src替换为 blank.gif,背景为none,用filter加载img标签原来的src)

介绍:虽说是纯CSS解决方案,但是也使用了JavaScript来运算,只不过是将脚本写到了CSS文件中,遗憾的是,此方案只支持img标签,对背景图片无效。


2、在需要设置透明的样式中加入下方代码,其中蓝色标注代码为刚才下载的透明图片,路径同样还是相对于HTML文件的位置 (不相对于CSS文件!):
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 = "images/blank.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);
}


方案4 - 原生JavaScript解决方案:

介绍:利用了方案1的滤镜原理来实现,但由于此javascript没有读取css文件中的样式,所以此方案同样只支持img标签,对背景图片无效。

2、由于此js只有使用IE6时才有用,所以为了让我们的页面更加高效的执行,我们可以将上方代码修改如下,只有IE6的时候才调用执行此JavaScript:
<!--[if IE 6]><script type="text/javascript" src="js/iepngfix.js"></script><![endif]-->


优点:
代码看起来似乎很优雅,基本没有外加的文件,效率还算不错。


缺点:
1、额外加入了js文件,增加http请求;
2、不支持背景图即Background;
3、当文件载入之前,会先暂时呈现灰底;
4、不支持Hover等伪类;


使用情况:
1、大部分透明的png存在于img标签中时可考虑;
2、如果有背景图的可以参考上面所说的支持背景图的两种方式;


方案5 - jQuery解决方案:

介绍:jQuery为我们带来了很大的方便,jQuery没有让我们有太大的失望,img和png都同时得以支持,唯一美中不足的还是无法平铺,无法使用CSS Sprite。


1、首先下载此方案所用到的js文件和透明gif jQueryPngFix.zip (2.7 KB, 下载次数: 1989)
2、找到js文件中找到blankgif: 'images/blank.gif',将路径修改为相对于HTML文件的位置 (不相对于CSS或js文件!)
3、由于此js只有使用IE6时才有用,所以为了让我们的页面更加高效的执行,我们可以将上方代码修改如下,只有IE6的时候才调用执行此JavaScript:
<!--[if IE 6]><script type="text/javascript" src="js/pngfix.js"></script><![endif]-->


优点:
1、CSS代码看起来很优雅,只需要引入js进行简单的配置一下就行了,效率还算不错;
2、支持背景图,支持img;


缺点:
1、额外加入了js文件和图片文件,增加http请求;
2、加载了一个庞大的jQuery类库;
3、多库共存的时候可能会出现问题;
4、不支持平铺;
5、不支持CSS Sprite;
6、当文件载入之前,会先暂时呈现灰底;
7、不支持Hover等伪类;


使用情况:
当您的项目中使用jQuery的时可以考虑;


方案6 - PNG8格式的图片解决方案:

介绍:png8和gif都是8位的透明度,IE6与生俱来就支持png8的索引色透明度,但不支持png或8位以上的 alpha 透明度。而对于非动画的GIF建议你使用PNG8,因为体积会更小~


方案7 - DD_belatedPNG解决方案: 推荐方案

介绍:我们都知道在目前所用的png图片透明解决方案基本都是使用滤镜、expression解决的、透明gif替代。但是这些方法都有一个缺点,就是不支持CSS中backgrond-position与background-repeat。而这次的js插件使用了微软的VML语言进行绘制且不需要引入其他文件,一个小小的js就可以完美解决png图片bug就连img标签和hover伪类也可以很好的解决。

<!--[if IE 6]> 
<script src="DD_belatedPNG.js"></script> _fcksavedurl=""DD_belatedPNG.js"></script>" 
<script> 
/* EXAMPLE */ 
DD_belatedPNG.fix('.png_bg'); 
/* 将 .png_bg 改成你应用了透明PNG的CSS选择器 */ 
</script> 
<![endif]--> 
使用a:hover请留意 

想要用透明PNG作为a:hover时的背景图片,,需要以”a:hover”来作为选择器 

例: 

复制代码代码如下:

<!--[if IE 6]> 
<script type="text/javascript" src="js/DD_belatedPNG.js" ></script> 
<script type="text/javascript"> DD_belatedPNG.fix('.png_bg,.box a:hover'); 
</script> 
<![endif]--> 

 

 

转载于:https://www.cnblogs.com/stephenykk/archive/2013/06/12/3132596.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值