欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript實(shí)現(xiàn)獲取img的原始尺寸的方法詳解

 更新時(shí)間:2023年03月31日 08:31:51   作者:來(lái)了老弟  
在微信小程序開(kāi)發(fā)時(shí),它的image標(biāo)簽有一個(gè)默認(rèn)高度,這樣你的圖片很可能出現(xiàn)被壓縮變形的情況,所以就需要獲取到圖片的原始尺寸對(duì)image的寬高設(shè)置,本文就來(lái)分享一下JavaScript實(shí)現(xiàn)獲取img的原始尺寸的方法吧

在前端開(kāi)發(fā)中我們幾乎不需要獲取img的原始尺寸,因?yàn)橹灰悴豢桃庠O(shè)置圖片的寬高它都會(huì)按照最佳比例渲染。但是在微信小程序開(kāi)發(fā)時(shí),它的image標(biāo)簽有一個(gè)默認(rèn)高度,這樣你的圖片很可能出現(xiàn)被壓縮變形的情況,所以就需要獲取到圖片的原始尺寸對(duì)image的寬高設(shè)置。

微信小程序獲取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,微信官方文檔 這個(gè)需要添加業(yè)務(wù)域名,服務(wù)端做接口驗(yàn)證。比較繁瑣不推薦。

瀏覽器中獲取圖片尺寸的方法

<!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;  這種寫(xiě)法沒(méi)什么卵用 */ 
    }
  </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) // 如果在標(biāo)簽行內(nèi)樣式?jīng)]有設(shè)置 無(wú)法獲取到

    // 2. 直接獲取DOM元素的width和height屬性
    console.log(img.width)        // 300 獲取到的數(shù)字類(lèi)型
    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()對(duì)象異步獲取圖片原始尺寸
    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}
    })()

    // 終極兼容寫(xiě)法 (首先檢測(cè)瀏覽器是否支持img.naturalWidth,如果支持直接獲取,不支持使用4.Image()對(duì)象獲?。?
    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實(shí)現(xiàn)獲取img的原始尺寸的方法詳解的文章就介紹到這了,更多相關(guān)JavaScript獲取img原始尺寸內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論