• 【原创】网页中图片等比例缩放(完美无错版)


    <SCRIPT LANGUAGE="JavaScript">

      <!--

      
      var iOriginalWidth, iOriginalHeight; //存放图片原始长度、宽度

      //动态显示图片的长度、宽度和显示比例

      function dispCurrentSize()

      {

      var zoom = document.all.idImg.style.zoom;

      zoom = eval(zoom.substring(0, zoom.length-1));

      var iWidth = zoom * document.all.idImg.width;

      var iHeight = zoom * document.all.idImg.height;

      

      }

      //显示图片原始长度、宽度

      function dispOriginalSize(oImg,x,y)

      {

      iOriginalWidth = oImg.width;

      iOriginalHeight = oImg.height;
           if  (iOriginalWidth > x || iOriginalHeight > y )
           {
             oImg.width  = x;
             oImg.height = oImg.height*(x/oImg.width)
             if ( oImg.height > y)
             {
                   iOriginalHeight = oImg.height
                   oImg.height = y;
                   oImg.width  = oImg.width*(y/iOriginalHeight);
            }
     
           }

      document.all.idImg.style.zoom = "100%";

      dispCurrentSize();

      }
     
      //-->

      </SCRIPT>



    <img src="http://www.seed-china.com/20051231news.jpg" width="150" border="0" ID="idImg"
    onLoad="dispOriginalSize(this,150,180);">




    下面是在网上看到的

    JS代码(随便放哪里):
    <script language="JavaScript">
    <!--
    var flag=false;
    function DrawImage(ImgD){
    var image=new Image();
    image.src=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="图片" border=0 width="180" height="110" onload="javascript drawImage(this);">
    width="180" height="110" 注意这里最好限定,如果不限定加载图时会成原大,然后再缩小,这个过程如果图大了很难看的.这里是宽度和高度,在前面的JS里改,这里也作相应的改.



    还有一种方法(没试过,呵呵。有兴趣的试下):

    style=table-layout:fixed;word-break:break-all


  • 相关阅读:
    在docker中执行linux shell命令
    ASP.NET MVC Controller接收ajax post方式发送过来的json对象或数组数据
    EasyUI 1.4.4 DataGrid(大数据量) bufferview滚动时不加载下一页数据解决方案
    MVC 5 下,应用log4net收集异常信息
    ASP.NET 获取IP信息等探针
    Discuz!NT 3.5.2正式版与Asp.net网站会员信息整合
    打印HTML页面部分区域javascript代码
    微网站之浅见
    从客户端检测到危险的Request.Form值解决方案
    Ajax客户登陆验证
  • 原文地址:https://www.cnblogs.com/MaxIE/p/319189.html
Copyright © 2020-2023  润新知