圖片自動縮小的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.dbjr.com.cn/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.dbjr.com.cn/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.dbjr.com.cn/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.dbjr.com.cn/htmldata/2006-01-08/圖片" border=0 width="180" height="110" onload="javascriptrawImage(this);">
width="180" height="110" 注意這里最好限定,如果不限定加載圖時會成原大,然后再縮小,這個過程如果圖大了很難看的.這里是寬度和高度,在前面的JS里改,這里也作相應的改.
圖不會變形,只會按比列縮,放心
相關(guān)文章
JavaScript判斷表單提交時哪個radio按鈕被選中的方法
這篇文章主要介紹了JavaScript判斷表單提交時哪個radio按鈕被選中的方法,實例分析了javascript操作表單radio按鈕的技巧,非常具有實用價值,需要的朋友可以參考下2015-03-03JavaScript基礎(chǔ)之文件上傳與下載的實現(xiàn)詳解
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)文件上傳與下載功能的相關(guān)資料,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以學習一下2023-01-01JavaScript isArray()函數(shù)判斷對象類型的種種方法
我們知道,JavaScript中檢測對象類型的運算符有:typeof、instanceof,還有對象的constructor屬性2010-10-10ES6中的Promise對象與async和await方法詳解
Promise是es6引入的異步編程薪解決方案,語法上promise就是一個構(gòu)造函數(shù),用來封裝異步操作病可以獲取其成功或失敗的結(jié)果,這篇文章主要介紹了ES6中的Promise對象與async和await方法,需要的朋友可以參考下2022-12-12