版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/coder_girl/article/details/85260394
/**
@params {imgBox} Number 图片容器的dom
@params {thisImg} Number 图片的dom
*/
changeImage(imgBox, thisImg, url) {
var maxWidth = imgBox.clientWidth
var maxHeight = imgBox.clientHeight
var image = new Image()
image.src = thisImg.src
image.onload = function () {
var width = image.width
var height = image.height
if (width > maxWidth || height > maxHeight) {
if (maxWidth / maxHeight <= image.width / image.height) {
width = maxWidth
height = maxWidth * (image.height / image.width);
} else {
width = maxHeight * (image.width / image.height);
height = maxHeight
}
}
thisImg.width = width
thisImg.height = height
}
}