• JavaScript 经典代码(14)


    <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="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>

  • 相关阅读:
    1 < 2 < 3为true, 3 > 2 > 1为false
    我的第五代选择器Icarus
    浮动不换行
    教你游泳,不会游的看了包你学会!!! 分享
    node.js 一个简单的页面输出
    天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,增益其所不能
    setTimeout和setInterval的使用
    JS window.open()属性
    车牌识别及验证码识别的一般思路
    苹果开发者账号注册流程
  • 原文地址:https://www.cnblogs.com/MaxIE/p/305082.html
Copyright © 2020-2023  润新知