我现在有个图片是通过js读取出来得。类似下面得这样<img style="position: absolute; left: 2384px; top: 0px; display: none;" src="../3.jpg">
<img style="position: absolute; display: block; z-index: 6; left: 0px; top: 0px; opacity: 1;" src="../4.jpg">
<img style="position: absolute; top: 0px; left: -2384px; display: none; z-index: 5;" src="../5.jpg">
<img style="position: absolute; top: 0px; left: 0px; display: none; z-index: 3; opacity: 1;" src="../1.jpg">
<img style="position: absolute; top: 0px; left: 0px; display: none; z-index: 2;" src="../2.jpg">这些图片都没有长度和高度~~~所以图片得尺寸太大得话,就显示不全了,我需要判断下,大得图片就缩小,小的尺寸不变我怎么样能得到图片实际得大小?
高手帮忙

解决方案 »

  1.   

    var img = new Image(); img.onload = function(){ alert(this.width); };  img.src="1.jpg";
      

  2.   

    利用下面的js获取
    document.images[0].onload=function(){
    alert(this.width);
    }
    但是,必须首先让图片可见,如果display为none,加上document.images[0].style.display ="block"。
      

  3.   

    document.images[0].onload=function(){
      //保证图片小于50*50px
      this.width=(this.width>50?50:this.width);
      this.height=(this.height>50?50:this.height);
    }
      

  4.   

    虽然img标签的属性里没有指定高度和宽度,但是只要这个图片在页面上显示出来了,那么仍然是可以取到宽度和高度的,而且可以手动设定它的宽度和高度。
      

  5.   


    <img onload="resize(this,100)" style="position: absolute; left: 2384px; top: 0px; display: none;" src="../3.jpg">
    //function resize(img, size, callback)
    //{
    //    var n = new Image();
    //    n.src = img.src;//    if (Browser.msie) ok();
    //    else n.onload = function ()
    //    {
    //        if (n.complete == true) ok();
    //    }//    function ok()
    //    {
    //        var w = n.width, h = n.height;
    //        if (w >= size) { h = size * h / w; w = size; }
    //        if (h >= size) { w = size * w / h; h = size; }
    //        img.width = w; img.height = h;
    //        if (callback) callback.call(img, size, [n.width, n.height]);
    //        n = null;
    //    }
    //};
      

  6.   

    上面的都可以,要等图片加载完毕后才能获取高度、宽度
    图片有onload事件的