直接获取尺寸
<img src="/img.png" class="image">
let image = document.querySelector("img");
window.getComputedStyle(image,null).height
间接获取图片尺寸
MDN文档 Image
通过Image对象
<img src="/img.png" class="image">
let image = document.querySelector("img");
image.onload = function(){
// 显示尺寸
this.width
this.height
// 图片携带的尺寸信息
this.naturalWidth
this.naturalHeight
}
或
let img = new Image()
img.src = "/img.png"
image.onload = function(){
// 显示尺寸
this.width
this.height
// 图片携带的尺寸信息
this.naturalWidth
this.naturalHeight
}
image对象
console.dir(image)