Picking up the height of an image

0

In my project, I have an image that reduces the screen 1000px, 860px, 320px, etc. The image also changes its height.

How do I get the height at the time of the display?

I need to know this time to set margin-top of a div that goes below it.

    
asked by anonymous 17.10.2016 / 19:59

1 answer

1

Two alternatives, using .getBoundingClientRect and getComputedStyle .

var img = document.querySelector('img');
var altura = img.getBoundingClientRect().height;
var altura2 = window.getComputedStyle(img).height;
console.log(altura, altura2);

jsFiddle: link

Ideally, you should have the element cached with var img = document... and then when you need to know the dimensions use one of the options above. If you explain better how you change the size of the images you have, I can give you a more complete / adapted example.

    
17.10.2016 / 20:13