1

I've read multiple posts on this, and even found a plugin (http://dreamerslab.com/blog/en/get-hidden-elements-width-and-height-with-jquery/), but still didn't fix my problem.

I need to get the actual height and width of an image, before it's displayed on the front end.

I've tried so many things, I really don't want to show it, then figure it out, then hide it again and then later show it. It doesn't seem practical, I was wondering if anyone has found any solutions?

I've tried:

$('img').get(0).clientWidth;
$('img').get(0).naturalWidth;
$('img').get(0).width;
$('img').get(0).offsetWidth;
$('img').get(0).width();

No luck with either of these, I did try the above with height as well.

The image DOESN"T have the height and width attributes set manually.

Shannon

Shannon Hochkins
  • 9,930
  • 11
  • 50
  • 88

6 Answers6

4

Nothing with display:none has dimensions. If you need it measurable but hidden, use

visibility: hidden

It will also take space in the flow of the document. If you don't want that, you can temporarily change its position to absolute.

Juan Mendes
  • 80,964
  • 26
  • 138
  • 189
  • I didn't want to 'hide and show it', I thought the has to be a better option, but thankyou. – Shannon Hochkins May 08 '13 at 01:20
  • @ShannonHochkins You have to render an image to get its dimensions in JS, no getting around that... At least this renders it in a way that doesn't disrupt the flow – Juan Mendes May 08 '13 at 05:16
0

The easiest way to achieve what you want is to append this images to the DOM hidden with CSS as the default style:

visibility: hidden

I like to use Jquery ImagesLoaded Plugin to determine when the images are ready like so:

//container holding your images
$('.photos').imagesLoaded(function () {
    //Function to measure size

    //Take measurements
    var w = $('img').width();
    var h = $('img').height();

    //Once you know the proper size, show it
    $('img').show();              
});
Matt
  • 901
  • 5
  • 12
0

Hey You can get Height and width from Jquery . I have made demo for you.

$(this).height();
$(this).width();

Please refer link

Demo

Neeraj Dubey
  • 4,193
  • 7
  • 25
  • 47
0
Community
  • 1
  • 1
ahmed alnahas
  • 263
  • 1
  • 4
0

In many cases you have to know the size of an image that is into a hidden element. It can be the image itself or a far parent.

I did this method to know wich one is on display : none;

function findHiddenParent(element)
{
    if (element.parent().is(':hidden'))
    {
        return findHiddenParent(element.parent());
    }
    else
    {
        return element;
    }
 }

And I use it that way :

if (img.is(':hidden'))
{
    hiddenParent = findHiddenParent(img);
    hiddenParent.show().css('visibility','hidden');
}
//Get width and height from img (that is a var getted from a jQuery selector

//When things done with width and height
if (hiddenParent != undefined)
{
    hiddenParent.css('visibility','visible').hide();
}
jona303
  • 541
  • 3
  • 16
-1

Seeing as there's really only two options that I can see:

  1. Cloneing the item, position it so that it's invisible to the user then get it's dimensions.
  2. Visually hide it, but keep it's presence in the DOM and get it's dimensions from there.

Both aren't great solutions, but to anyone who would like to use it:

HTML:

<img id="hidden" src="http://www.shannonhochkins.com/_images/513330a8965f59b83a00034d" />

CSS:

#hidden {display:none; margin:10px;}

JAVASCRIPT:

$.fn.actual = function(type, includeMargin){
    var elem = $(this),
        unit = 0;
        includeMargin = (includeMargin ? true : false);
    elem.css({position: 'absolute', visibility: 'visible', display: 'block'});
    switch(type) {
        case 'height':
            unit = elem.height();
        break;
        case 'width':
            unit = elem.width();
        break; 
        case 'outerHeight':
            unit = elem.outerHeight(includeMargin);
        break;
        case 'outerWidth':
            unit = elem.outerWidth(includeMargin);
        break;  
        case 'innerWidth':
            unit = elem.innerWidth(includeMargin);
        break;   
        case 'innerHeight':
            unit = elem.innerHeight(includeMargin);
        break;             
    }
    elem.css({position: 'static', visibility: 'visible', display: 'none' });
    return unit;
};

var height = $('#hidden').actual('height');
var width = $('#hidden').actual('outerWidth', true);
alert('Width: ' + width + ' height: ' + height);

DEMO:

http://jsfiddle.net/bXG5n/

Shannon

Shannon Hochkins
  • 9,930
  • 11
  • 50
  • 88