js擷取圖片原始大小

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   sp   strong   div   

摘要:

  瀏覽器中顯示的圖片大小未必是他真實的高和寬,比如像下面這樣,我們給他加上寬和高的樣式

<img src="IE.png" style="width:25px;height:25px;">

  這樣在瀏覽器中顯示的大小就是25px。那麼我們如何擷取圖片的真實大小呢?,下面的代碼就實現了這個功能

 1 <!DOCTYPE html> 2 <html> 3     <head> 4         <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> 5     </head> 6     <body> 7         <img src="IE.png" id="image" style="width:25px;height:25px;">      8         <script> 9             // 設定延時保證圖片載入完成10             setTimeout(function() {11                 var12                 real_width,13                 real_height,14                 _im         = document.getElementById(‘image‘),15                 im          = document.createElement(‘img‘);16                 im.src      = _im.src,17                 real_width  = im.width,18                 real_height = im.height;19                 alert(real_width+‘\n‘+real_height);20             },500);21         </script>22     </body>23 </html>

 

 

注意:上面代碼本人在IE7+和chrome上都測試過了,因為沒有裝IE6,所以沒法測試。

 

js擷取圖片原始大小

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.