JS+style實現動態設定圖片高寬

來源:互聯網
上載者:User

<script type="text/javascript">
<!--
function resizeIMG(){
var objImg = document.getElementById("pictureID");//pictureID是圖片ID號
var img_w = objImg.width;
var img_h = objImg.height;

var w=parseInt(img_w);var h=parseInt(img_h);

//預設圖片最寬400px,最高300px,可根據需要自行修改
if( w>400 ) objImg.style.width = "400px";
if( h>300 ) objImg.style.height = "300px";
}
//-->
</script>
將以上JS代碼放在<head>中
然後將<body>改成<body onload="resizeIMG()">

 在網上找了很多動態改變圖片高寬的代碼,都不行,偶然看到有人說用style來設定高寬的,經我實踐,果然可行

即使在img標籤中設定了高寬,也可改變,看來就算是設定了img的width,height,也可用style修正

我是用的win2003 + IE6 + Maxthon 1.6.1,測試通過,其它瀏覽器沒有測試

以上代碼還可以擴充,比如按比例縮小圖片,有需要的可以自行修改

今天總算有些收穫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.