js获取图片的原始尺寸

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript" src="Packages/jQuery.1.8.3/jquery-1.8.3.min.js"></script>
</head>
<body>
    <img id="imgT" style="width:500px;height:150px" src="http://img11.360buyimg.com/da/g14/M07/01/0E/rBEhVlNhh8wIAAAAAADmFBLo1twAAM26gOmCgYAAOYs716.jpg"/>
    <input id="Text1" type="text" style="padding:1px;margin:1px;height:30px;width:50px" value="ceshi" />
    <script>
        //获取图片的长宽,会受css的width与height影响。
        //获取的结果是css的width与height
        //看下:http://www.cnblogs.com/chengxiaohui/articles/1873605.html
        $(function () {
            $('img').load(function () {
                //var msg = "width:" + $(this).width() + "height:" + $(this).height();
                var msg = "width:" + this.width + "height:" + this.height;
                alert(msg);
            })
        })
        //获取图片原始尺寸
        //load方法绑定给window对象时,会在所有内容加载后触发,包括窗口,框架,对象和图像。
        //$(document).ready()是在所有DOM节点加载完毕后执行。
        //而往往在DOM节点加载完毕后,img图象还未完全加载完毕的。
        //此处使用$(document).ready()获取的长宽都是0,采用$(window).load()
        $(window).load(function () {
            ////方式一
            //$("<img/>").attr("src", $('#imgT').attr("src")).load(function () {
            //    var msg = "width:" + this.width + "height:" + this.height;//获取长宽:this.width 。不能用$(this).width()
            //    alert(msg);
            //})

            ////方式二
            var img = $('#imgT');
            var theImage = new Image();
            theImage.src = img.attr('src');
            var msg = "width:" + theImage.width + "height:" + theImage.height;//长宽theImage.width。不能用$(theImage).width()
            alert(msg);
        })
        //jQuery中 height(width) innerHeight(innerWidth) outerHeight(outerWidth)的区别
        $(function () {
            var width = "width():" + $("#Text1").width() +
                "\r\ninnerWidth():" + $("#Text1").innerWidth() +
                "\r\nouterWidth():" + $("#Text1").outerWidth() +
                "\r\nouterWidth():" + $("#Text1").outerWidth(true);
            alert(width);
            
        })

        //此为通过图片头数据获取长宽
        //原址:http://www.planeart.cn/?p=1121
        //demo:http://www.planeart.cn/demo/imgReady/
        // 更新:
        // 05.27: 1、保证回调执行顺序:error > ready > load;2、回调函数this指向img本身
        // 04-02: 1、增加图片完全加载后的回调 2、提高性能

        /**
         * 图片头数据加载就绪事件 - 更快获取图片尺寸
         * @version    2011.05.27
         * @author    TangBin
         * @see        http://www.planeart.cn/?p=1121
         * @param    {String}    图片路径
         * @param    {Function}    尺寸就绪
         * @param    {Function}    加载完毕 (可选)
         * @param    {Function}    加载错误 (可选)
         * @example imgReady('http://www.google.com.hk/intl/zh-CN/images/logo_cn.png', function () {
                alert('size ready: width=' + this.width + '; height=' + this.height);
            });
         */
        var imgReady = (function () {
            var list = [], intervalId = null,

            // 用来执行队列
            tick = function () {
                var i = 0;
                for (; i < list.length; i++) {
                    list[i].end ? list.splice(i--, 1) : list[i]();
                };
                !list.length && stop();
            },

            // 停止所有定时器队列
            stop = function () {
                clearInterval(intervalId);
                intervalId = null;
            };

            return function (url, ready, load, error) {
                var onready, width, height, newWidth, newHeight,
                    img = new Image();

                img.src = url;

                // 如果图片被缓存,则直接返回缓存数据
                if (img.complete) {
                    ready.call(img);
                    load && load.call(img);
                    return;
                };

                width = img.width;
                height = img.height;

                // 加载错误后的事件
                img.onerror = function () {
                    error && error.call(img);
                    onready.end = true;
                    img = img.onload = img.onerror = null;
                };

                // 图片尺寸就绪
                onready = function () {
                    newWidth = img.width;
                    newHeight = img.height;
                    if (newWidth !== width || newHeight !== height ||
                        // 如果图片已经在其他地方加载可使用面积检测
                        newWidth * newHeight > 1024
                    ) {
                        ready.call(img);
                        onready.end = true;
                    };
                };
                onready();

                // 完全加载完毕的事件
                img.onload = function () {
                    // onload在定时器时间差范围内可能比onready快
                    // 这里进行检查并保证onready优先执行
                    !onready.end && onready();

                    load && load.call(img);

                    // IE gif动画会循环执行onload,置空onload即可
                    img = img.onload = img.onerror = null;
                };

                // 加入队列中定期执行
                if (!onready.end) {
                    list.push(onready);
                    // 无论何时只允许出现一个定时器,减少浏览器性能损耗
                    if (intervalId === null) intervalId = setInterval(tick, 40);
                };
            };
        })();

    </script>
</body>
</html>
<!--说明-->
<!--jQuery中 height(width) innerHeight(innerWidth) outerHeight(outerWidth)的区别-->
<!--
    height(width):高度(宽度)
    innerHeight(innerWidth):高度(宽度)+内边距(padding)
    outerHeight(outerWidth):高度(宽度)+内边距(padding)+边框
    outerHeight(outerWidth) 参数为true时:高度(宽度)+内边距(padding)+边框+外边距(margin)
    -->

posted on 2014-05-04 13:26  儿时精神  阅读(731)  评论(0编辑  收藏  举报

导航