備忘(圖片的等比例縮放)

openjava 13年前發布 | 2K 次閱讀

<PRE style="BACKGROUND-COLOR: #c5c5c5; FONT-WEIGHT: bold" class=javascript name="code">function resizeimg(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.heightiwidth)/image.width; }else{ ImgD.width=image.width; ImgD.height=image.height; } ImgD.alt=image.width+"×"+image.height; } else{ if(image.height>iheight){ ImgD.height=iheight; ImgD.width=(image.widthiheight)/image.height; }else{ ImgD.width=image.width; ImgD.height=image.height; } ImgD.alt=image.width+"×"+image.height; }      ImgD.style.cursor= "pointer"; //改變鼠標指針      ImgD.onclick = function() { window.open(this.src);} //點擊打開大圖片     if (navigator.userAgent.toLowerCase().indexOf("ie") > -1) { //判斷瀏覽器,如果是IE       ImgD.title = "請使用鼠標滾輪縮放圖片,點擊圖片可在新窗口打開";       ImgD.onmousewheel = function img_zoom() //滾輪縮放       {           var zoom = parseInt(this.style.zoom, 10) || 100;           zoom += event.wheelDelta / 12;           if (zoom> 0) this.style.zoom = zoom + "%";           return false;       }     } else { //如果不是IE         ImgD.title = "點擊圖片可在新窗口打開";        } } }</PRE>

html部分
<img src="圖片url" onload="javascript:resizeimg(this,500,600);"  />
網上抄來的東西,寫上以免留在電腦中忘記了存放的地方(只支持ie內核的瀏覽器)

 
 本文由用戶 openjava 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!