taro3.x: 圖片自適應處理(mode屬性)


微信小程序的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)
            })
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM