圖片適配容器大小
阿新 • • 發佈:2018-12-26
/** @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 } }