PHP & JavaScript Room :: 設置サンプル

実行結果

PNG Opacity Fix for IE6 | 設置サンプル

▼jQueryを使用。 まずブラウザ判定をし、透過PNGに対応していないIE5.5またはIE6の場合に、ページ内のsrc属性の拡張子がPNGのimg要素にDirectXのAlphaImageLoaderを適用させています。

設置サンプルのソース

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <meta http-equiv="Content-Language" content="ja" />
      <meta http-equiv="Content-Script-Type" content="text/javascript" />
      <meta http-equiv="Content-Style-Type" content="text/css" />
      <meta http-equiv="imagetoolbar" content="no" />
      <title>IE PNG Fix | 設置サンプル</title>
      <link rel="stylesheet" type="text/css" href="/content/lib/global.css" />
      <!-- JS -->
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
      <script type="text/javascript">
         /* 置換する透過GIF画像 */
         var blank=new Image();
         blank.src='/content/img/blank.gif';

         $(function() {
            /* IE5.5、IE6かどうか判定 */
            var badBrowser=(/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
            if (badBrowser){
               /* img要素のsrc属性の値で拡張子PNGのimg要素に対して透過PNGを対応させる */
               $('img[src$=.png]').each(function() {
                  if (!this.complete) {
                     this.onload=function(){ fixPng(this) };
                  }else{
                     fixPng(this);
                  }
               });
            }
         });
         function fixPng(png) {
            /* PNG画像のパスを取得 */
            var src = png.src;
            /* 幅と高さを設定 */
            if (!png.style.width) { png.style.width = $(png).width(); }
            if (!png.style.height) { png.style.height = $(png).height(); }
            /* 透過GIF画像に置換 */
            png.src = blank.src;
            /* フィルター設定(オリジナル画像表示) */
            png.runtimeStyle.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"',sizingMethod='scale')";
         }
      </script>
   <link rel="stylesheet" type="text/css" href="/common/css/example.css"></head>
   <body id='example3' class='example'><div class="ads" style="margin:32px auto;text-align:center;"></div><h1 class='h'><a href='/'>PHP &amp; JavaScript Room</a> :: 設置サンプル</h1>
<h3 class='h'>実行結果</h3>
      <div id="wrap">
      <h1><a href='http://docs.jquery.com/Tutorials:PNG_Opacity_Fix_for_IE6'>PNG Opacity Fix for IE6</a> | 設置サンプル</h1>
      <p>▼jQueryを使用。 まずブラウザ判定をし、透過PNGに対応していないIE5.5またはIE6の場合に、ページ内のsrc属性の拡張子がPNGのimg要素にDirectXのAlphaImageLoaderを適用させています。</p>
<!-- CODE -->
      <p><img src="/content/img/dog_white.png" alt="" /></p>
<!-- / CODE -->
   </body>
</html>