JavaScript實現(xiàn)獲取img的原始尺寸的方法詳解
在前端開發(fā)中我們幾乎不需要獲取img的原始尺寸,因為只要你不刻意設置圖片的寬高它都會按照最佳比例渲染。但是在微信小程序開發(fā)時,它的image標簽有一個默認高度,這樣你的圖片很可能出現(xiàn)被壓縮變形的情況,所以就需要獲取到圖片的原始尺寸對image的寬高設置。
微信小程序獲取image原始尺寸的方法
<view style="width:100%;" > <image src="https://sf3-ttcdn-tos.pstatp.com/img/mosaic-legacy/3796/2975850990~300x300.image" bindload="loadSuccess" style="width:{{imageWidth}}px; height:{{imageHeight}}px"></image> </view>
//js Page({ data: { imageHeight: 0, imageWidth: 0 }, loadSuccess(e){ const { detail: {width, height} } = e // // 這里獲取到的就是圖片原始尺寸 this.setData({ imageWidth: width, imageHeight:height }) } })
wx.getImageInfo
方法是wx.getImageInfo,微信官方文檔 這個需要添加業(yè)務域名,服務端做接口驗證。比較繁瑣不推薦。
瀏覽器中獲取圖片尺寸的方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>img原始尺寸獲取</title> <style> .image { /* height: 20px; 這種寫法沒什么卵用 */ } </style> </head> <body> <img class="image" referrerpolicy="no-referrer" src="https://image-static.segmentfault.com/193/916/1939169050-641cff9f16cdc_fix732" style="width: 300px;"> <script> // 1. 獲取DOM元素的渲染尺寸 const img = document.querySelector('.image'); console.log(img.style.width) // 300px 獲取到字符串 console.log(img.style.height) // 如果在標簽行內(nèi)樣式?jīng)]有設置 無法獲取到 // 2. 直接獲取DOM元素的width和height屬性 console.log(img.width) // 300 獲取到的數(shù)字類型 console.log(img.height) // 533 可以獲取到元素的渲染高度 // 3. naturalWidth / naturalHeight (適用于Firefox/IE9/Safari/Chrome/Opera瀏覽器) console.log('naturalWidth:', img.naturalWidth) // naturalWidth: 412 console.log('naturalHeight:', img.naturalHeight) // naturalHeight: 732 // 4. 使用Image()對象異步獲取圖片原始尺寸 function getImageInfo(url) { return new Promise((resolve, reject) => { let image = new Image(); image.onload = () => { resolve({ width: image.width, height: image.height }) } image.onerror = () => { reject(new Error('image load error')) } image.src = url; }) } (async () => { let size = await getImageInfo('https://image-static.segmentfault.com/193/916/1939169050-641cff9f16cdc_fix732') console.log(size) // {width: 412, height: 732} })() // 終極兼容寫法 (首先檢測瀏覽器是否支持img.naturalWidth,如果支持直接獲取,不支持使用4.Image()對象獲取) async function getImageSize(img) { if (img.naturalWidth) { return { width: img.naturalWidth, height: img.naturalHeight } } else { return await getImageInfo(img.src) } } </script> </body> </html>
到此這篇關(guān)于JavaScript實現(xiàn)獲取img的原始尺寸的方法詳解的文章就介紹到這了,更多相關(guān)JavaScript獲取img原始尺寸內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS+XML 省份和城市之間的聯(lián)動實現(xiàn)代碼
用JS來操作一個XML文檔來實現(xiàn)一個簡單的表單聯(lián)動2009-10-10JavaScript編寫實現(xiàn)飛機大戰(zhàn)
這篇文章主要為大家詳細介紹了JavaScript編寫實現(xiàn)飛機大戰(zhàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05JavaScript 雙向鏈表操作實例分析【創(chuàng)建、增加、查找、刪除等】
這篇文章主要介紹了JavaScript 雙向鏈表操作,結(jié)合實例形式分析了JavaScript雙向鏈表的創(chuàng)建、增加、查找、刪除等相關(guān)操作技巧,需要的朋友可以參考下2020-04-04JavaScript中join()、splice()、slice()和split()函數(shù)用法示例
這篇文章主要介紹了JavaScript中join()、splice()、slice()和split()函數(shù)用法,結(jié)合實例形式較為詳細的分析了join()、splice()、slice()和split()函數(shù)的功能、使用方法及相關(guān)操作注意事項,需要的朋友可以參考下2018-08-08微信小程序?qū)隫ant報錯VM292:1 thirdScriptError的解決方法
這篇文章主要給大家介紹了關(guān)于微信小程序?qū)隫ant報錯VM292:1 thirdScriptError的解決方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用微信小程序具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-08-08