JavaScript等比例縮放圖片控制超出範圍的圖片_javascript技巧

來源:互聯網
上載者:User
js等比例縮放圖片,這個功能非常實用,當網頁載入一個尺寸比較大的圖片時,往往會把一個網頁撐的變形,頁面變得很難看,於是我們就想到了用JS去控制超出一定範圍的圖片,雲棲社區以穩定頁面配置,本程式碼片段就是完成了此功能,而且代碼非常簡潔,效果很好。
複製代碼 代碼如下:

<html><head><title>等比例縮放圖片</title><script>function
DrawImage(ImgD,iwidth,iheight){ //參數(圖片,允許的寬度,允許的高度)
var
image=new Image(); image.src=ImgD.src;
if(image.width>0
&& image.height>0){ if(image.width/image.height>=
iwidth/iheight){ if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
}
</script></head><body><img

src=http://www.jb51.net/uploadfile/2013/0803/20130803034531502.jpg"
alt="自動縮放後的效果"

width="100"

height="100"

onload="javascript:DrawImage(this,80,80)"

/></body></html>

聯繫我們

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