-
-
Save megurock/cbdcfeeeb871c6d7f7b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
*/ | |
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