public
Created

Fix PNGs in IE < 9 jQuery plugin

  • Download Gist
jquery.fixPNGs.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
;(function fixPNGsPlugin($){
/* Fixes PNGs in IE < 9 for use in fading and other opacity changes.
* Adapted from:
* http://stackoverflow.com/questions/1204457/how-to-solve-hack-fading-semi-transparent-png-bug-in-ie8/4126528#4126528
* http://stackoverflow.com/questions/1156985/jquery-cycle-ie7-transparent-png-problem/1157006#1157006
*/
var $blankImg = 'transparent_1x1.gif'
, $sizingMethod = 'crop';
$.fn.fixPNGs = function()
{
if (!$.browser.msie || $.browser.version >= 9) return this;
this.each(function forEachElement(){
if (DD_belatedPNG && $.browser.version < 8) {
DD_belatedPNG.fixPng(this);
return;
}
var isImg = $.nodeName(this, 'img')
, path = (isImg) ? this.src : this.currentStyle.backgroundImage;
// If the path is surrounded by `url(` and `)`...
if (path.search(/^url\(/i) !== -1) {
// ...extract the path
path = path.match(/^url\(['"]?([^'"]+)['"]?\)\s*$/i)[1];
}
// Make sure this is a PNG image
if (path.search(/\.png$/i) === -1) return;
// Apply the filter
this.style.filter =
"progid:DXImageTransform.Microsoft.AlphaImageLoader" +
"(src='" + path + "', sizingMethod='" + $sizingMethod + "')";
// Replace the background image with a transparent 1x1 image.
if (isImg) this.src = $blankImg;
else {
this.style.backgroundImage = 'url(' + $blankImg + ')';
this.style.backgroundRepeat = 'repeat';
}
});
return this;
}; // $.fn.donutSlider()
})(jQuery); // (fixPNGsPlugin)()

Added DD_belatedPNG since it handles PNGs in IE <= 7 much better and I couldn't get just the jQuery plugin working with IE7.

This isn't working when changing the opacity of images. They still show the transparent parts black.

Hmm, it's working for me. What browsers are you having troubles with and what code are you using to invoke the plugin?

Can you plz put an example of how to used.

Tks in advance

$('.ants li img, .cats li.has_png_background').fixPNGs();

Does that help?

It works but IE8 is throwing an error at char 461? (Invalid argument)

update: It seems to fail completely in IE7?

I'm targeting multiple elements, some of which are children of the first affected element with child selector (>) don't know if that would cause it any issues?

I'm not sure what to tell you without more information about the issues you're experiencing. I only modified these plugins, I didn't write them. You can follow the links in the comments to the original authors. What you described doesn't sound like it would be an issue. I would suggest trying to apply this to a single element to see if that works and if it does, move on from there until you can nail down the exact combinations that cause the problems.

RE: Error at character 461 - you need to force IE8 into ie7 rendering mode in order to use the pngfix: http://mezzoblue.com/archives/2010/05/20/ie8_still_fa/

vespoli is correct. Note the comment at the top of DD_belatedPNG.js. I noted that it should be included using conditional comments for IE <= 7. The jquery.FixPNGs.js file handles IE8.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.