IE6 PNG transparency

坚强是说给别人听的谎言 提交于 2019-11-26 22:11:31

I like this Javascript solution writen by David Cilley some time ago. It gets out of the way of compliant browsers and can be used with any back-end you want. It does still require a blank gif image though.

Add these functions to your HTML Header or other existing .js include:

<script type="text/javascript">
    function fixPngs(){
    // Loops through all img tags   
        for (i = 0; i < document.images.length; i++){
            var s = document.images[i].src;
            if (s.indexOf('.png') > 0)  // Checks for the .png extension
                fixPng(s, document.images[i]);
        }
    }

    function fixPng(u, o){
        // u = url of the image
        // o = image object 
        o.src = 'images/spacer.gif';  // Need to give it an image so we don't get the red x
        o.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + u + "', sizingMethod='scale')";
    }   
</script>

Put the following conditional comment at the bottom (footer section, just before </body> ):

<!--[if lte IE 6]>
    <script language="javascript" type="text/javascript">
        //this is a conditional comment that only IE understands. If a user using IE 6 or lower 
         //views this page, the following code will run. Otherwise, it will appear commented out.
        //it goes after the body tag so it can fire after the page loads.
        fixPngs();
    </script> 
<![endif]-->

For a more comprehensive approach to IE6 oddities, try the IE7 Javascript library.

Use this: http://www.twinhelix.com/css/iepngfix/

This is also good for IE 5.5, but not for mac versions of IE or earlier version of IE.

I've used it on quite few sites and have had no problems with it.

There can sometimes be an ugly grey box around the PNG however until the script kicks in.

Use PNG Behaviour.

ie6.css:

img {
   behavior: url("pngbehavior.htc");
}

page.html:

<!--[if IE 6]> 
  <link rel="stylesheet" type="text/css" href="ie6.css" />
<![endif]-->

I'm going for the Necromancer badge here. :)

This isn't exactly a fix, but a handy workaround I'm using from time to time to support transparency on IE6 without any extra code at all. It won't always fit the bill, but pretty often it does.

The idea is that most of the time you will need to show your PNGs on a fixed color background anyway. A PNG file can have a background color specified, and IE6 will honor it. What you do is that you get the TweakPNG utility and open up your PNG file. There you will be able to add a bKGD chunk, which specifies the desired background color. Just type in the color that you will need to show the PNG on, and you're done.

One note - PNG files often include some values to compensate for different display devices. There might be things like intended color spaces, chromacities, gamma, etc. These aren't bad as per se, but IE somehow misinterpreted them, and the result was that the PNGs showed up darker than they should have been (or maybe IE was the only one who got it right? I don't remember...)

Anyway, if you want every browser to display your PNGs the same, you should delete these chunks with the above mentioned utility.

The twinhelix png fix should help you

For example with Dean Edwards' ie7.js

Be aware that the AlphaImageLoader transform can deadlock IE6.

Use PNG8s instead of regular PNG32s. You are restricted to 256 colors and 1 bit of alpha transparency, but it beats randomly deadlocking the browser.

You could be brave and simply state that your site may not render well on IE6. Perhaps not the most commercially minded approach but we'd do all of ourselves a favor (even Microsoft) if we just let IE6 die. Of course since a large amount of online activity happens on corporate machines with IE6 nailed to them that isn't really going to happen soon. :(

1.Add conditional css for IE6 inside the head block of your document:

<!--[if (IE 6)]>
    <link rel="stylesheet" type="text/css" href="locationOfyourCss/ie6.css"/>
<![endif]-->

2.Assign class name in your element:


<td class = yourClassName>

3.In ie6.css, apply filter. You have to specify width and height. Set background image to clear.cache.gif, the filter doesn't work without those properties:


.yourClassName{
    width:8px;
    height:22px;
    background: url("locationOfBlankImage/clear.cache.gif");
    filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='filePath/imageName.png', sizingMethod='scale');
}

What I'm doing in a project I'm working on is taking a cue from Paul Irish's HTML5 Boilerplate and assigning conditional styles to the entire page. He goes into it in detail here, but briefly, the idea is to add conditional checks in the HTML of every page on your site, applied to the body tag. Like so:

<!--[if lt IE 7 ]> <body class="ie6 pageStyle"> <![endif]-->
<!--[if IE 7 ]>    <body class="ie7 pageStyle"> <![endif]-->
<!--[if IE 8 ]>    <body class="ie8 pageStyle"> <![endif]-->
<!--[if IE 9 ]>    <body class="ie9 pageStyle"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <body class="pageStyle"> <!--<![endif]-->

IE browsers will read these comments and apply those styles. Other browsers will ignore it. The genius of this is you can design standards compliant websites, use PNGs, whatever. And then, in your CSS, add additional styles placed AFTER your standard styles to give IE browsers what they want. For instance, one style I'm working on uses a PNG background image. To make this play nice in ie6, I COULD use javascript/htc to replace them, apply one of the many hacks out there. OR I could do this:

.someStyle {
    background: url(/images/someFile.png) no-repeat; 
background-position: -0px -280px;

}

.ie6 .someStyle {
   background: url(/images/someFile.gif) no-repeat; 
background-position: -0px -280px;

}

By feeding a GIF to my ie6 users, there's no CPU hogging workaround processes, which is actually a very big issue if you've ever tested on the kind of POS machine likely to be running ie6. And, I haven't given up any quality for my nice users with nice browsers.

It does require two separate files, but I think it's a much cleaner implementation than most. Also, instead of a separate ie6.css file, using .ie6 .whatever keeps your fixes right next the the styles they apply to, which I find cleaner and easier to use. It also encourages you to consider ie6 users as you go, instead of finishing your design and then looking back in horror

This is a great article about this problem. In summary, it provides a JS library called SuperSleight. I have used it in the past with a decent amount of success.

Ok, I'm virtually new to html/js but I think I had to change Rob Allen's answer a bit to fix two problems:

  1. AlphaImageLoader() was changing the aspect ratio of my images so I needed to restore the original sizes AFTER the images were loaded.
  2. fixPngs(), if called from the end of the html, was being called after the document was loaded but before all the images were loaded.

So I've changed my fixPngs() to just attach an event:

function fixPngs() {
  // Loops through all img tags
  for(i = 0; i < document.images.length; i++) {
    var u = document.images[i].src;
    var o = document.images[i];

    if(u.indexOf('.png') > 0) {
      o.attachEvent('onload', fixPng);
      o.src = u;
    }
  }
}

fixPngs() is still being called from the end of the html:

<!--[if lte IE 6]>
  <script language="javascript" type="text/javascript">
    // Do this after the page loads
    fixPngs();
  </script>
<![endif]-->

And fixPng() now fetches the caller, detaches the event, saves the original dimensions, calls AlphaImageLoader() and finally restores the dimensions:

// u = url of the image
// o = image object
function fixPng(e) {
  var caller = e.target            ||
               e.srcElement        ||
               window.event.target ||
               window.event.srcElement;
  var u = caller.src;
  var o = caller;

  if(!o || !u)
    return;

  // Detach event so is not triggered by the AlphaImageLoader
  o.detachEvent('onload', fixPng);

  // Save original sizes
  var oldw = o.clientWidth;
  var oldh = o.clientHeight;

  // Need to give it an image so we don't get the red x
  o.src          = 'images/spacer.gif';
  o.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ u +"', sizingMethod='scale')";

  // Restore original sizes
  if(o.style) {
    o.style.width  = oldw +"px";
    o.style.height = oldh +"px";
  }
}

Have I overkilled it?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!