zoukankan      html  css  js  c++  java
  • 通过JS,按照原比例控制图片尺寸

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>Js压缩图片</title>
        <script type="text/javascript" >
            //img:图片对象
            //   w:宽度
            //   h:高度
            function ctlImg(img, w, h) {
                var img_Width = w;
                var img_Height = h;
                var w = img.width;
                var h = img.height;
                if (img.width > img_Width && img.height > img_Height) {
                    if (w / img_Width > h / img_Height) {
                        img.width = img_Width;
                        img.height = parseInt(img_Width / w * h);
                    }
                    else {
                        img.height = img_Height;
                        img.width = parseInt(img_Height / h * w);
                    }
                    return true;
                }
                if (w > img_Width) {
                    img.width = img_Width;
                    img.height = parseInt(img_Width * (h / w));
                } else if (h > img_Height) {
                    img.height = img_Height;
                    img.width = parseInt(img_Height * (w / h));
                }
                return true;
            }
    </script>
    </head>
    <body >
    <img alt="" src="1.jpg "  onload="ctlImg(this,80,60)"/>
    <img alt="" src="2.jpg "  onload="ctlImg(this,800000,600)"/>
    <img alt="" src="3.jpg "  onload="ctlImg(this,80,60)"/>
    <img alt="" src="4.jpg "  onload="ctlImg(this,8000,100)"/>

    </body>
    </html>

  • 相关阅读:
    pigeon
    servlet
    Linux (centos6.5) 安装Node和pm2
    git 常用命令
    git本地仓库推送代码到远程仓库
    linux 服务器 磁盘空间查看清理
    阿里云 Kubenetes容器 时区相差8小时 设置环境变量
    推荐一个简易易懂的ElasticSearch 入门学习站
    ElasticSearch 设置索引mapping 文档类型,重置elastic密码
    linux 查看系统各项指标(资源,内存)
  • 原文地址:https://www.cnblogs.com/chengeng/p/3143559.html
Copyright © 2011-2022 走看看