问答详情
源自:3-6 获取图片尺寸

下载过来的图片高度和长度都为:0;

您好,为什么我写的代码attr("src",sourceSrc)中的src传不过来,以致图片的高度和长度都为:0;

能帮忙解释下吗,谢谢!

提问者:yuyuda 2015-07-21 14:20

个回答

  • 一只帅蚂蚁
    2015-07-21 15:22:38
    已采纳

    这种情况下,一般回事图片还没有加载完成就获取尺寸,还有就是图片是否已经在DOM结构里面,如果单纯创建Image对象,没有插入到页面,通常会出现这种情况,注意~~

  • 一只帅蚂蚁
    2015-07-21 15:20:30

    贴出代码看下

  • yuyuda
    2015-07-21 15:26:22

    loadPicSize:function(sourceSrc){
                var self=this;
                this.preLoadImg(sourceSrc,function(){
                    self.popupPic.attr("src",sourceSrc);
                    var picWidth=self.popupPic.width(),
                        picHeight=self.popupPic.height();
                    self.changePic(picWidth,picHeight);
                });
            },
    preLoadImg:function(src,callback){
                var img=new Image();
                if(!!window.ActiveXObject){
                    img.onreadystatechange=function(){
                        if (this.readyState=="complete") {
                            callback();
                        };
                    };
                }else {
                    img.onload=function(){
                        callback();
                    };
                };
                img.src=src;
            },