views:

24

answers:

2

I tried the following code:

    var origh1 = $('img:first').height();
    alert(origh1);

But it alerted a value of '0'.

My img tag does not have a height value set, so is that why it is returning 0?

If so, how do I retrieve the value of the height of the image ?

Also, I noticed that when I do an animate to increase the size of the image by 20% and reduce it 20%, it ends up being a little larger than the original. How do I restore the image to it's original size? And why does it not go back to original if I reduce it 20%. I know that there is Math involved...but how do I compensate for that in jQuery?

This is my code for that:

$('img:first').fadeIn('normal').delay(500).animate({'height':'+=20%'}, 1500, 'easeOutQuad')

And the return to original:

$('img:first').fadeIn('normal').animate({'height':'-=20%'}, 1000, 'swing');
+2  A: 

Make sure the image properly loads before you query it.

$(function() {
     alert( $('img').eq(0).height() )
});

You can also try binding it to the window load event if DOM Ready is too soon:

$(window).load(function() {
     alert( $('img').eq(0).height() )
});
meder
As for the first suggestion...isn't that the exact same thing as`alert($('img:first').height(););` ? That was the first thing I tried, but it still returned 0.I also tried moving it to the end of the document - ensuring that the images loaded before it ran, but that didn't work either.As for the binding it to the window load event, is this scalable if I have 100 images being loaded on the same page? Do I bind 100 pages to the window load event?
marcamillion
Moving your code down isn't going to execute it when all the images load unless you wrap it in my latter example. Sounds like you need to bind it to window load.
meder
Putting it further down in your document will not keep the script from running before your images load. The windows.load event simply fires when all of your content is done loading. I don't think it matters how much content you have. Not sure what you mean about "bind it to 100 pages".
Greg
That's what I wanted to know...about the binding. Greg...I meant bind it to 100 window load.Thanks guys.
marcamillion
Any suggestions on the second half of my question? Why does it not reduce it to the original size?
marcamillion
You could iterate through and explicitly set the width and height with `.css` to attempt to make sure it goes back to its original dimensions. Not really sure.
meder
+2  A: 

Try

$(window).load(function(){
    $('img:first').attr("height")
});

This will fire after your images have fully loaded

edit -- Sorry, I had to edit that about five times. I keep making silly mistakes today.

Greg
Sall good.This works, for the most part, I believe you left off a trailing bracket and semi-colon :)
marcamillion