Skip to content

Instantly share code, notes, and snippets.

@megurock
Forked from nowri/fixIE8Alpha.js
Last active August 29, 2015 14:04
Show Gist options
  • Save megurock/cbdcfeeeb871c6d7f7b3 to your computer and use it in GitHub Desktop.
Save megurock/cbdcfeeeb871c6d7f7b3 to your computer and use it in GitHub Desktop.
/**
*
*/
function doIEHack() {
var $fixNodes = $('.pngfix');
// appy filter for all image elements with a 'pngfix' class being attached
$fixNodes.filter('img').each(function() {
applyFilterOnNode($(this));
});
// appy filter for all background images whose element have a 'pngfix' class (image element excluded)
$fixNodes.not('img').each(function(i, el) {
applyFilterOnBackgroundImage($(this));
});
}
/**
*
*/
function applyFilterOnBackgroundImage($node) {
var regex = /url\(["']*(.*?)["']*\)/;
var src = $node.css("background-image").replace(regex, "$1");
if (src !== 'none') {
console.log("fix background " + src);
$node.css({
backgroundImage: "none",
filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '", sizingMethod="scale")'
});
} else {
console.log("backgroundImage is none!");
}
}
/**
*
*/
function applyFilterOnNode($node) {
var src = $node.attr('src');
if (src.indexOf('.png') !== -1) {
console.log("fix node " + src);
$node.css({
filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '", sizingMethod="scale")'
});
}
}
/**
*
*/
function isIEHackRequired() {
return (typeof window.addEventListener === 'undefined' && typeof document.getElementsByClassName === 'undefined');
}
if (isIEHackRequired()) {
doIEHack();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment