- 新版浏览器
// 这个 api 仅支持新版本浏览器, 旧版还是得创建一个内部图片
setTimeout(() => {
let imgRef = this.$refs.img
this.imgWidth = imgRef.naturalWidth
this.imgHeight = imgRef.naturalHeight
}, 10)
- 旧版浏览器(兼容)
function getImgNaturalDimensions(img, callback) {
var nWidth, nHeight
if (img.naturalWidth) { // 现代浏览器
nWidth = img.naturalWidth
nHeight = img.naturalHeight
} else { // IE6/7/8
var imgae = new Image()
image.src = img.src
image.onload = function () {
callback(image.width, image.height)
}
}
return [nWidth, nHeight]
}
标签:浏览器,img,image,naturalWidth,js,获取,imgRef,nHeight
From: https://www.cnblogs.com/whm-blog/p/17443630.html