JavaScript获取图片(Image)的大小(宽度,高度)

阅读更多
如果只有图片的URL
function getImageDimensions(imgStyleRule) {
  var defaultSize, img;
  defaultSize = {
    width: 10,
    height: 30
  };
  if (!imgStyleRule || !imgStyleRule.style || !imgStyleRule.style.backgroundImage) {
    return defaultSize;
  }
  img = new Image();
  img.src = imgStyleRule.style.backgroundImage.replace(/url\(|\)$|"/ig, '');
  return {
    width: img.width,
    height: img.height
  };
}


[size=medium]
如何获取imgStyleRule, 参考
http://darrenzhu.iteye.com/admin/blogs/2065946


如果图片已经加载,可以通过如下方式获取
var img = document.getElementById('imageid'); 
//or however you get a handle to the IMG
var width = img.clientWidth;
var height = img.clientHeight;

[/size]

你可能感兴趣的:(image,img,size,width,宽度)