限制图片大小

function CheckImg(img)
{

  var MaxWidth=400;//设置图片宽度界限
  var MaxHeight=400;//设置图片高度界限
  var HeightWidth=img.offsetHeight/img.offsetWidth;//设置高宽比
  var WidthHeight=img.offsetWidth/img.offsetHeight;//设置宽高比 
 /*if(img.readyState!="complete"){
  return false;//确保图片完全加载
  }*/
  if(img.offsetWidth>MaxWidth){
   img.width=MaxWidth;
   img.height=MaxWidth*HeightWidth;
  }
  if(img.offsetHeight>MaxHeight){
   img.height=MaxHeight;
   img.width=MaxHeight*WidthHeight;
  }
}
发布了11 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/liuyingsv/article/details/50914435