今天终于在网上找到一篇等级缩放图片的程序,现拿出来给大家分享:
在head加入
<
script language
=
"
JavaScript
"
>
<!--
var flag = false ;
function DrawImage(ImgD){
var image = new Image();
image.src = ImgD.src;
if (image.width > 0 && image.height > 0 ){
flag = true ;
if (image.width / image.height >= 164 / 112 ){
if (image.width > 164 ){
ImgD.width = 164 ;
ImgD.height = (image.height * 164 ) / image.width;
} else {
ImgD.width = image.width;
ImgD.height = image.height;
}
ImgD.alt = image.width + " × " + image.height;
}
else {
if (image.height > 112 ){
ImgD.height = 112 ;
ImgD.width = (image.width * 112 ) / image.height;
} else {
ImgD.width = image.width;
ImgD.height = image.height;
}
ImgD.alt = image.width + " × " + image.height;
}
}
}
// -->
</ script >
<!--
var flag = false ;
function DrawImage(ImgD){
var image = new Image();
image.src = ImgD.src;
if (image.width > 0 && image.height > 0 ){
flag = true ;
if (image.width / image.height >= 164 / 112 ){
if (image.width > 164 ){
ImgD.width = 164 ;
ImgD.height = (image.height * 164 ) / image.width;
} else {
ImgD.width = image.width;
ImgD.height = image.height;
}
ImgD.alt = image.width + " × " + image.height;
}
else {
if (image.height > 112 ){
ImgD.height = 112 ;
ImgD.width = (image.width * 112 ) / image.height;
} else {
ImgD.width = image.width;
ImgD.height = image.height;
}
ImgD.alt = image.width + " × " + image.height;
}
}
}
// -->
</ script >
然后这样子调用:
<
a
href
="链接地址"
target
="_blank"
><
img
src
="图片地直址"
border
="0"
onload
="javascript:DrawImage(this);"
></
a
>
经测试非常稳定,希望能帮助更多人解决这个问题。。。