1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Element.addMethods({
pngHack: function(el){
var el = $(el);
var transparentGifPath = '/images/transparent.gif';
if (Prototype.Browser.IE){
/* if it's an img and a png */
if ((el.match('img')) && (el.src.include('png'))){
var alphaImgSrc = el.src;
var sizingMethod = 'scale';
el.src = transparentGifPath;
/* if it's an element with a background png */
} else if (el.getStyle('background-image').include('png')){
var elBg = el.getStyle('background-image');
var alphaImgSrc = elBg.slice(5, elBg.length - 2);
var sizingMethod = 'crop';
el.setStyle({ 'background': transparentGifPath });
}
if (alphaImgSrc) el.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="#{alphaImgSrc}",sizingMethod="#{sizingMethod}")'.interpolate({
alphaImgSrc: alphaImgSrc, sizingMethod: sizingMethod
});
}
return el;
}
});
Refactorings
No refactoring yet !
getopenid.com/deleteme
November 16, 2007, November 16, 2007 17:28, permalink
It now retains background colors.
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
Element.addMethods({
pngHack: function(el){
var el = $(el);
var transparentGifPath = '/images/transparent.gif';
if (Prototype.Browser.IE){
/* if it's an img and a png */
if ((el.match('img')) && (el.src.include('png'))){
var alphaImgSrc = el.src;
var sizingMethod = 'scale';
el.src = transparentGifPath;
/* if it's an element with a background png */
} else if (el.getStyle('background-image').include('png')){
var bgColor = '';
if (el.getStyle('background-color')) bgColor = el.getStyle('background-color') + ' ';
var elBg = el.getStyle('background-image');
var alphaImgSrc = elBg.slice(5, elBg.length - 2);
var sizingMethod = 'crop';
el.setStyle({ 'background': bgColor + transparentGifPath });
}
if (alphaImgSrc) el.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="#{alphaImgSrc}",sizingMethod="#{sizingMethod}")'.interpolate({
alphaImgSrc: alphaImgSrc, sizingMethod: sizingMethod
});
}
return el;
}
});
Makes 32 bit PNG's transparency work in Internet Explorer 6
* Dependent on Prototype 1.6
* Works on img elements and on background images of elements
* Image tiling will not work
Example Usages:
$('yourPNG').pngHack();
$$('div#fixMe', 'img#andMe', 'img.andUsTo').invoke('pngHack');
Feel free to make any changes you like.