JavaScript等比例縮放圖片
時間:2016-03-29 17:34來源:未知 作者:admin
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="*.gif" alt="自動縮放后的效果" width="220" height="80" onload="javascript:DrawImage(this,100,100)" />
</body>
</html>
|
------分隔線----------------------------
- 推薦內容
-
- div+css網頁設計的css命名方案
用CSS 布局設計網頁的時候,命名需要良好的規劃,如果隨心所欲的...
- 網頁設計一定要有利于網絡營銷
一、針對搜索引擎友善的設計,也就是基本的網站搜索引擎優化...
- div+css網頁設計的css命名方案