微信小程序的image標簽中有個mode屬性,使用aspectFill即可
注:image組件默認寬度300px、高度225px
mode 有效值:
mode 有 13 種模式,其中 4 種是縮放模式,9 種是裁剪模式
mode: 'aspectFit' 的處理模式:
const handleLoaded = (e) => { const maxWidth = Taro.getSystemInfoSync().windowWidth const maxHeight = 280 const ratio = maxWidth / maxHeight const realWidth = e.detail.width const realHeight = e.detail.height const imgRatio = realWidth / realHeight if (ratio > imgRatio) { setStlye({ width: realWidth * (maxHeight / realHeight), height: maxHeight }) } else { setStlye({ width: maxWidth, height: realHeight * (maxWidth / realWidth) }) } }