Getting image width and height with jquery

前端 未结 3 542
不思量自难忘°
不思量自难忘° 2021-01-16 14:40

I have a very simple code, which annoyingly was working and for the life of me I can not see why it is now failing:

function imageSize(img){
  var theImage =         


        
3条回答
  •  孤独总比滥情好
    2021-01-16 15:13

    You have to wait , till your image will be loaded , and then access to its sizes:

    function imageSize(img){
      var theImage = new Image();
      theImage.onload = function(){
        var imgwidth = theImage.width;
        var imgheight = theImage.height;
    
        alert(imgwidth+'-'+imgheight);
      }
      theImage.src = img.attr('src');
    
    }
    

提交回复
热议问题