Will
Will

Reputation: 681

get different value when using getComputedStyle

I want to get a image height by using getComputedStyle, but when i refresh the page, it shows different values. Sometimes it shows the value i want, sometimes it shows 0px. Why this happens, and how can i fix it?

I also try the width, position, and i got right values of them.

Below is the code

javascript:

var imageMajor = document.images[0];
var height = document.defaultView.getComputedStyle(imageMajor, null).height;

css:

div#gallery div#image_wrapper div#image_holder img#major {
    position: absolute;
    width: 50%;
    float: left;
    border: none;
}

Upvotes: 1

Views: 192

Answers (1)

Ross McLellan
Ross McLellan

Reputation: 1881

You may need to put the JS into a window.onload event (or some other onload/timer), as it may be firing before the image has loaded (until the image loads it will have a height of 0)

Upvotes: 1

Related Questions