本文實例講述了js實現兼容IE、Firefox的圖片縮放代碼。分享給大家供大家參考,具體如下:
function SetSize(obj, width, height) {
myImage = new Image();
myImage.src = obj.src;
if (myImage.width > 0 && myImage.height > 0) {
var rate = 1;
if (myImage.width > width || myImage.height > height) {
if (width / myImage.width < height / myImage.height) {
rate = width / myImage.width;
} else {
rate = height / myImage.height;
}
}
if (window.navigator.appName == "Microsoft Internet Explorer") {
obj.style.zoom = rate;
} else {
obj.width = myImage.width * rate;
obj.height = myImage.height * rate;
}
}
}
用法:
復制代碼 代碼如下:<img src="img/offer/41936519.jpg" border="0" style="zoom: 0.1" onload="SetSize(this, 80, 60)"/>
這種方法在IE、FIREFOX、OPERA、NETSCAPE測試都適用。
希望本文所述對大家JavaScript程序設計有所幫助。