zoukankan      html  css  js  c++  java
  • JS使图片在图片框中自适应,按比例缩放

    <script type="text/javascript">

            //ImgD:要放图片的img元素,onload时传参可用this

            //h:img元素的高度,像素

            //w:img元素的宽度,像素
            function autosize2(ImgD,h,w)
            {
                var image=new Image(); 
                image.src=ImgD.src; 
                if (image.width<w && image.height<h)
                {
                    ImgD.width=image.width;
                    ImgD.height=image.height;
                }
                else
                {
                    if (w / h <= image.width / image.height)
                    {
                        ImgD.width=w;
                        ImgD.height=w * (image.height / image.width);
                    }
                    else
                    {
                        ImgD.width=h * (image.width / image.height);
                        ImgD.height=h;
                    }
                }
                
                //图片居中,IE8有效果,IE9,火狐无效果,请在页面用table居中
                //ImgD.style.paddingLeft = (w + 20 - ImgD.width) / 2;   //20是指padding-left和padding-right距离的和
                //ImgD.style.paddingTop=(h + 20 -ImgD.height) / 2;     //20是指padding-top和padding-bottom距离的和
            }
        </script>

         在图上加载时调用这个函数(img的onload事件)

  • 相关阅读:
    Navicat 远程连接ubuntu出现的问题
    替换 ubuntu 自带的python版本
    xpath疑惑
    xpath中返回值问题
    AttributeError: 'unicode' object has no attribute 'xpath'
    linux下mysql忘记密码解决方案
    IntelliJ idea常用快捷键
    最近的说明(本篇不谈具体技术,看技术的可以忽略)
    常用的排序算法介绍和在JAVA的实现(二)
    mysql数据库查询过程探究和优化建议
  • 原文地址:https://www.cnblogs.com/wsir/p/5577273.html
Copyright © 2011-2022 走看看