ie 6 pngピクチャの透明表示を実現する方法

18179 ワード

ie 6 PNGピクチャの透明化を実現する方法:一、フィルタ(png背景ピクチャの透明表示を実現する)1、正常なCSSコードを書き、backgroundを通じてピクチャをインポートし、このようにすべてのブラウザがこのPNGピクチャを使用した.background:url(../images/W3CfunsLogo.png); しかしie 6の下のpng画像が透明なところには薄い背景があります.
2、フィルタによって画像を導入する、フィルタによって画像を導入する場合、CSSファイルではなくHTMLファイルに対して、文法は以下の通りである:filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/W3CfunsLogo.png");
次のようになります.
#pics { background:url(../images/W3CfunsLogo.png) no-repeat;
/*以下はIE 6にPNG透明コードを設定*/background:none; _filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/W3CfunsLogo.png"); }
シナリオ2-HTCプラグインソリューション:
紹介:IE 5.5バージョンから、インターネット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:' +

            '

<' + 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に対して絶対パスでもOK
~~~原理的にはalphaImageLoaderフィルタでpngの透明性を実現し、元の背景図background-imgまたはsrcは
 
シナリオ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はIE 6を使用する場合にのみ有用であるため、私たちのページをより効率的に実行するために、上のコードを以下のように変更することができ、IE 6の場合にのみこのJavaScript:
利点:コードは優雅に見え、ほとんど追加されていないファイルで、効率はまあまあです.
欠点:1、jsファイルを追加し、httpリクエストを追加します.2、背景図であるBackgroundはサポートされていない.3、ファイルがロードされる前に、しばらく灰色の底が現れます.4、Hoverなどの偽類をサポートしない;
使用状況:1、ほとんどの透明なpngがimgラベルに存在する場合に考慮できる.2、背景図がある場合は、上述した背景図をサポートする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はIE 6を使用する場合にのみ使用できますので、私たちのページをより効率的に実行するために、上のコードを以下のように変更することができます.このJavaScript:
利点:1、CSSコードは優雅に見え、jsを導入して簡単な構成をすればいいだけで、効率は悪くない.2、背景図をサポートし、imgをサポートする.
欠点:1、jsファイルとピクチャファイルを追加し、httpリクエストを追加する.2、巨大なjQueryクラスライブラリをロードした.3、マルチライブラリが共存している場合、問題が発生する可能性がある.4、タイルはサポートされていません.5、CSS Spriteをサポートしない;6、ファイルをロードする前に、まず一時的に灰色の底を現す.7、Hoverなどの偽類をサポートしない;
使用状況:プロジェクトでjQueryを使用する場合に考慮できます.
シナリオ6-PNG 8形式のピクチャソリューション:
紹介:png 8とgifはいずれも8ビットの透明度で、IE 6は生まれつきpng 8のインデックス色の透明度をサポートしているが、pngや8ビット以上のalpha透明度はサポートしていない.アニメーションではないGIFではPNG 8を使うことをお勧めします.体積が小さくなるからです.
シナリオ7-DD_belatedPNGソリューション:推奨ソリューション
紹介:現在使用されているpngピクチャの透明ソリューションは、基本的にフィルタ、expressionを使用して解決された透明gifの代わりに使用されていることが知られています.しかし、これらの方法には、CSSにおけるbackgrond−positionとbackground−repeatがサポートされていないという欠点がある.今回のjsプラグインはマイクロソフトのVML言語を使って描画され、他のファイルを導入する必要がなく、小さなjsでpngピクチャバグを完璧に解決することができ、imgラベルやhover偽クラスでもよく解決することができます.