zoukankan      html  css  js  c++  java
  • JS等比例缩放图片,限定最大宽度和最大高度

    JS等比例缩放图片,限定最大宽度和最大高度


    JavaScript

    
    //图片按比例缩放
    var flag=false;
    function DrawImage(ImgD,iwidth,iheight){
        //参数(图片,允许的宽度,允许的高度)
        var image=new Image();
        image.src=ImgD.src;
        if(image.width>0 && image.height>0){
        flag=true;
        if(image.width/image.height>= iwidth/iheight){
            if(image.width>iwidth){  
            ImgD.width=iwidth;
            ImgD.height=(image.height*iwidth)/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.width*iheight)/image.height;        
            }else{
            ImgD.width=image.width;  
            ImgD.height=image.height;
            }
            ImgD.alt=image.width+"×"+image.height;
            }
        }
    } 
    
    


    html

    
    <img src="images/toplogo.gif" onload="javascript:DrawImage(this,100,100)">
    
    
  • 相关阅读:
    数据结构 1
    MyBatis 7
    MyBatis 6
    MyBatis 5
    MaBatis 4
    MyBatis 3
    目录和文件管理
    Linux常用命令精讲
    Sentos7.4安装说明
    RIP
  • 原文地址:https://www.cnblogs.com/qixidi/p/10287567.html
Copyright © 2011-2022 走看看