JavaScript 經典代碼(14)

來源:互聯網
上載者:User

<script language="JavaScript1.2">
/*
 This following code are designed and writen by Windy_sk <windy_sk@126.com>
 You can use it freely, but u must held all the copyright items!
*/
function Modi_Img(img,mode,the_num){
 try{
  if(img.tagName.toLowerCase() != "img") return;
 }catch(e){
  return;
 }
 if(mode!=3 && !/\d+(\.\d+)?/.test(the_num)) return;
 if(img.readyState=="complete"){
  img.style.height = "";
  img.style.width  = "";
  var org_w = img.offsetWidth;
  var org_h = img.offsetHeight;
  if(mode==3){
   img.style.width  = 10;
   img.style.height = 10;
   var the_parent = img.offsetParent;
   if(the_num==null){
    if(the_parent.offsetWidth/org_w > the_parent.offsetHeight/org_h){
     the_num = the_parent.offsetHeight;
     mode = 1;
    }else{
     the_num = the_parent.offsetWidth;
     mode = 0;
    }
   }else{
    img.style.width  = the_parent.offsetWidth;
    img.style.height = the_parent.offsetHeight;
   }
  }
  switch(mode){
   case 0: // zoom by width
    img.style.height = org_h*the_num/org_w;
    img.style.width  = the_num;
    break;
   case 1: // zoom by height
    img.style.width  = org_w*the_num/org_h;
    img.style.height = the_num;
    break;
   case 2: // zoom by rate
    img.style.width  = org_w*the_num;
    img.style.height = org_h*the_num;
    break;
   case 3: // zoom to fill the container
    break;
   default:
    img.style.width  = org_w;
    img.style.height = org_h;
  }
  img.alt = "";
  img.alt += "檔案:" + img.src;
  img.alt += '\n大小:' + img.fileSize + '位元組';
  img.alt += "\n寬:" + img.offsetWidth;
  img.alt += '\n高:' + img.offsetHeight;
 }
}

function zoom_show(obj,rate){
 if(!event.ctrlKey){
  obj.style.width = parseInt(obj.style.width)*(1+rate);
  obj.style.height = parseInt(obj.style.height)*(1+rate);
 }else{
  obj.style.width = parseInt(obj.style.width)/(1+rate);
  obj.style.height = parseInt(obj.style.height)/(1+rate);
 }
 Modi_Img(obj.firstChild,3);
}
</script>
點擊放大,按住 ctrl 點擊縮小<br>
<div style="width:150px; height:100px; border:black 1px solid; margin:0px; padding: 0px" onclick="zoom_show(this,0.2)" onselectstart="return false">
<img id=test src="http://www.iecn.net/forum/images/iecn/logo.gif" onreadystatechange="Modi_Img(this,3)"></div>

 

相關文章

聯繫我們

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