动态获取图片原始尺寸函数
const myHeight=184
const imageSize = (url) => {
const nImg = new window.Image()
nImg.src = url
return new Promise((resolve, reject) => {
nImg.onload = () => {
resolve({ success: true, width: nImg.width, height: nImg.height })
}
nImg.onerror = () => {
reject(new Error('imageError'))
}
})
}
useEffect(() => {
if (!src) return
imageSize(src).then((size) => {
const { width = 0, height = 1 } = size
const proportion = height / width
const h = proportion * myHeight
const w = myHeight / proportion
if (h < myHeight)) {
//如图片高度小于需要撑开的高度,则作相应处理
} else {
//反之。。
}
})
}, [src])