0

I am trying to fill a div with images and size the images to fit just right in their place. The problem is that when the image is not loaded, the dimensions x , y cannot be sampled from the element. This is what I am trying to do after looking up some examples on stack overflow. It does not work...

    $.each(data.data.children, function(i,item){
        // Some images come in with a busted link. Hence the IF statement...
        if (item.data.url.indexOf(".jpg")>0){
            var imgTitle = item.data.title;
            imgTitle = imgTitle;
            $('<div id="c'+i+'"></div>').attr("class", "imgDiv").appendTo("#photos");
            $("<img/>").attr("src", item.data.url).attr("id", "pic"+i).attr("class", "pics").appendTo("#c"+i);
                .load(function(){
                    $(this).fadein("slow");
                    if (iWidth>iHeight){
                        $('#pic'+i).width(270);
                        $('#pic'+i).height(parseInt(270-imgTitle.length/3));
                    }
                });

            $("<p>"+imgTitle+"</p>").attr("id", "p"+i).appendTo("#c"+i);  
            $("<img/>").attr("src", "images/RedX2.png").attr("class", "xit").appendTo("#c"+i);

            var mImg = $('#pic'+i)[0];
        }
    });

Alternatively, is there a way to do proportional fitting of an image inside the padding space proportionally?

TIA

DK

enter image description here

DeKoss
  • 431
  • 3
  • 16

1 Answers1

2

You might not need any javascript at all. See this post for a way to use CSS to have the image automatically size to the div that contains it.

How do I auto-resize an image to fit a div container

Community
  • 1
  • 1
HeadCode
  • 2,642
  • 1
  • 12
  • 24