Skip to content

Instantly share code, notes, and snippets.

@hasschi
Created March 9, 2015 10:12
Show Gist options
  • Save hasschi/ef24ace80c0be7c524a9 to your computer and use it in GitHub Desktop.
Save hasschi/ef24ace80c0be7c524a9 to your computer and use it in GitHub Desktop.
修正png在ie6底下顯示不正常 (需 x.gif檔)
var pngfix = function (option) {
if (true || navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
if (!document.getElementsByClassName) {
document.getElementsByClassName = function (classname) {
var all = document.documentElement.all || document.all;
var elem, list, ret = [];
for (var i = 0; i < all.length; i++) {
elem = all[i];
if (elem.className != "") {
list = elem.className.split(' ');
for (var j = 0; j < list.length; j++) {
if (list[j] == classname) {
ret.push(elem);
break;
}
}
}
}
return ret;
}
}
var target = (!!option && option.target) || [];
var exception = (!!option && option.exception) || [];
var checkskip = function (obj, arr) {
var skip = false;
var arr_class = [], arr_id = [], arr_tag = [];
for (var i = 0; i < arr.length; i++) {
var ex = arr[i];
if (ex.substr(0, 1) == "#") {
arr_id.push(ex.substr(1));
} else if (ex.substr(0, 1) == ".") {
arr_class.push(ex.substr(1));
} else {
arr_tag.push(ex);
}
};
if (obj.className != "") {
var classList = obj.className.split(' ');
for (var j = 0; j < classList.length; j++) {
for (k = 0; k < arr_class.length; k++) {
if (arr_class[k] == classList[j]) {
skip = true;
break;
}
}
if (skip) break;
}
}
if (obj.id != "") {
for (var k = 0; k < arr_id.length; k++) {
if (arr_id[k] == obj.id) {
skip = true;
break;
}
}
}
if (arr_tag.length) {
for (var k = 0; k < arr_tag.length; k++) {
if (arr_tag[k] == obj.tagName) {
skip = true;
break;
}
}
}
return skip;
};
var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
if (true || rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) <= 6) {
var all = document.documentElement.all || document.all;
var reg = /\s*(?:url\(\")?((([^:\/#@\?]+)?(?:\:\/\/)?[^\/#\?]*)?(([^#\?]*)?\/([^\.\/#\?]*))?(\.[^\.\/#\?\)\"]*)?)/;
for (var i = 0; i < all.length; i++) {
var obj = all[i],
bg = obj.currentStyle["background-image"],
offset, exec;
if (target.length != 0 && !checkskip(obj, target)) {
continue;
}
if (checkskip(obj, exception)) {
continue;
};
if (obj.src) {
exec = reg.exec(obj.src);
if (exec[7] == ".png") {
obj.src = "images/x.gif";
obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + exec[1] + "', sizingMethod='crop')";
obj.style["background-position"] = offset || "";
obj.style.width = obj.width + "px";
obj.style.height = obj.height + "px";
}
}
if (bg != "none") {
exec = reg.exec(bg);
if (exec[7].toLowerCase() == ".png") {
offset = obj.style["background-position"];
if (obj.currentStyle.backgroundRepeat != "repeat-x") {
obj.style.backgroundImage = "url('images/x.gif')";
obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + exec[1] + "', sizingMethod='crop')";
obj.style["background-position"] = offset || "";
}
}
}
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment