圖片自動縮小的js代碼,用以防止圖片撐破頁面
更新時間:2007年03月12日 00:00:00 作者:
JS代碼(隨便放哪里):
<script language="JavaScript">
<!--
var flag=false;
function DrawImage(ImgD){
var image=new Image();
image.src=http://www.dhdzp.com/htmldata/2006-01-08/ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= 180/110){
if(image.width>180){
ImgD.width=180;
ImgD.height=(image.height*110)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
/*ImgD.alt="bigpic" */
}
else{
if(image.height>110){
ImgD.height=110;
ImgD.width=(image.width*110)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
/*ImgD.alt="bigpic" */
}
}
}
//-->
</script>
圖片使用的地方:
<img src="http://www.dhdzp.com/htmldata/2006-01-08/圖片" border=0 width="180" height="110" onload="javascriptrawImage(this);">
width="180" height="110" 注意這里最好限定,如果不限定加載圖時會成原大,然后再縮小,這個過程如果圖大了很難看的.這里是寬度和高度,在前面的JS里改,這里也作相應的改.
圖不會變形,只會按比列縮,放心
<script language="JavaScript">
<!--
var flag=false;
function DrawImage(ImgD){
var image=new Image();
image.src=http://www.dhdzp.com/htmldata/2006-01-08/ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= 180/110){
if(image.width>180){
ImgD.width=180;
ImgD.height=(image.height*110)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
/*ImgD.alt="bigpic" */
}
else{
if(image.height>110){
ImgD.height=110;
ImgD.width=(image.width*110)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
/*ImgD.alt="bigpic" */
}
}
}
//-->
</script>
圖片使用的地方:
<img src="http://www.dhdzp.com/htmldata/2006-01-08/圖片" border=0 width="180" height="110" onload="javascriptrawImage(this);">
width="180" height="110" 注意這里最好限定,如果不限定加載圖時會成原大,然后再縮小,這個過程如果圖大了很難看的.這里是寬度和高度,在前面的JS里改,這里也作相應的改.
圖不會變形,只會按比列縮,放心
相關(guān)文章
javascript設(shè)計模式 – 策略模式原理與用法實例分析
這篇文章主要介紹了javascript設(shè)計模式 – 策略模式,結(jié)合實例形式分析了javascript策略模式相關(guān)概念、原理、用法及操作注意事項,需要的朋友可以參考下2020-04-04
JavaScript使用箭頭函數(shù)實現(xiàn)優(yōu)化代碼
在JavaScript的編程世界里,我們時常被普通函數(shù)的冗長寫法所困擾,每次都需要寫function關(guān)鍵字,有時候還要明確地寫return語句,下面我們就來看看如何利用箭頭函數(shù)優(yōu)化這些繁瑣的代碼吧2023-11-11

