Wednesday, 4 May 2011

Problems with jQuery fades using semi-transparent PNG images in IE8

I had some PNG images containing some semi-transparency (there were parts that were not completely transparent i.e. a shadow). I then used jQuery to fade between these images, as shown in the code below. This worked in all browsers except for IE, which showed a dark grey ring around each image while it was fading (it looked fine before and after each animation). I think IE was replacing each semi-transparent pixel with a fully opaque dark grey one. The solution was to apply Microsoft's AlphaImageLoader filter to each image, as shown in the CSS at the bottom.

HTML

JQUERY
setInterval(function() {
   var $active = $("#header-splats .active");
   var $next = $active.next().length ? $active.next() :
                                $("#header-splats div:first");
        
   $active.fadeOut("slow", function() {
      $active.removeClass('active');
   });
   $next.fadeIn("slow").addClass('active');
  
}, 3000);

CSS
#header-splats, #header-splats div {
   width: 106px;
   height: 106px;
}
/* overlap each other */
#header-splats div {
   position: absolute;
   top: 0;
   left: 0;
   display: none;
}
#header-splats div.active {display: block;}

This looks great in all browsers except IE, as you can see in these images:


THE SOLUTION: apply the following Microsoft filter in the CSS to each image (make sure you update the image path for each one).

CSS
#splat-1 img {filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='/images/splats/splat1.png');}
#splat-2 img {filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='/images/splats/splat2.png');}
#splat-3 img {filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='/images/splats/splat3.png');}

4 comments:

  1. Hey Jack, nice post.

    On my site, I use a JS based PNG fix for IE, seems to fix most PNG transparency related issues and works well with jQuery. Here is the code I use

    http://www.voodoochilli.com/scripts/pngfix.js

    I call it using an IF IE condition, for example



    I'd be interested to know if this resolves you issues as well as your own solution.

    P.S congratulations on the traffic levels.

    Harry

    ReplyDelete
  2. Oh seems like blogger strips out code...but you get the point.

    ReplyDelete
  3. Looking at the code, seems to do the exact same thing as your proposed solution anyway. Might be worth keeping it in an external file (if you haven't already)

    :)

    ReplyDelete
  4. Yer that script is useful for development and maintenance as you don't need to do anything for each individual image, so adding new ones etc is easy peasy.

    But I think in a lot of situations it will be overkill - if you only need to apply the MS filter on say 3 images on the whole site (as I did), and they never change, it is more efficient to just hardcode it in the CSS, rather than run a script every page load, scanning the DOM for image tags.

    ReplyDelete