zoukankan      html  css  js  c++  java
  • Tips:javascript 图片放大和取得尺寸

    1)获取图片尺寸

    <img src="http://img.my.csdn.net/uploads/201309/03/1378223257_7957.jpg" alt="MM" title="MM(实际大小200*300)" onclick="getWH(this)" width="200" height="300"/>
    <script>
    function getWH(t){
    	//DOM属性
    	console.log("width="+t.width);//200
    	console.log("height="+t.height);//300
    	//操作样式
    	console.log("styleWidth="+t.style.width);//空
    	console.log("styleHeight="+t.style.height);//空
    }


    2)获取图片尺寸(不设置宽高)

    <img src="http://img.my.csdn.net/uploads/201309/03/1378223257_7957.jpg" alt="MM" title="MM(实际大小200*300)" onclick="getWH(this)"/>
    <script>
    function getWH(t){
    	//DOM属性
    	console.log("width="+t.width);//200
    	console.log("height="+t.height);//300
    	//操作样式
    	console.log("styleWidth="+t.style.width);//空
    	console.log("styleHeight="+t.style.height);//空
    }


    我们只要不在style中显式地设置它,宽高永远为空!

    3)放大图片:

    这里我们利用了IE的私有属性防止图片放大失真严重!@司徒正美

    <img src="http://img.my.csdn.net/uploads/201309/03/1378223257_7957.jpg" alt="MM" title="MM(实际大小200*300)" onclick="getWH(this)" width="200" height="300"/>
    <script>
    function getWH(t){
    	t.width *= 2;
    	t.height *= 2;
    	//每点击一次,宽高放大一倍
    }
    </script>
    


    4)在FF与谷歌中,我们还可以用naturalWidth与naturalHeight取得图片的原大小!

    <img src="http://img.my.csdn.net/uploads/201309/03/1378223257_7957.jpg" alt="MM" title="MM(实际大小200*300)" onclick="getWH(this)" width="200" height="300"/>
    <script>
    function getWH(t){
    	console.log("width="+t.naturalWidth);
    	console.log("height="+t.naturalHeight);
    	t.width = t.naturalWidth * 2;
    	t.height = t.naturalHeight * 2;
    }
    </script>


    naturalWidth和naturalHeight只是只读属性,不能用来设置图片的大小,不能持续放大。

    附:MM.jpg  http://img.my.csdn.net/uploads/201309/03/1378223257_7957.jpg

  • 相关阅读:
    mysql binlog日志
    安装源码包(这里主要写了redis,其他都一样的操作)
    hashMap异常:java.util.ConcurrentModificationException
    谷歌post url “net::ERR_INCOMPLETE_CHUNKED_ENCODING”
    itextpdf结合jfinal模版生成pdf文件
    springboot项目jar冲突问题解决
    map遍历
    el和jstl表达式问题==0的问题
    fastJSON 解析转换包含下划线属性的问题
    CentOS 7 64位
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3301552.html
Copyright © 2011-2022 走看看