So I'm working on a Polymer element that is supposed to be responsive. For that I need access to the current image's width.
Here's my markup
<div class="meme">
<div id="memeImage"><img id="memeimg" src="{{img}}"></div>
</div>
And the (relevant) styling.
.meme {
position: relative;
-webkit-box-align:center;
-webkit-box-pack:center;
display:-webkit-box;
}
#memeImage {
z-index: -1;
border: 0px;
margin: 0px;
text-align: center;
position: relative;
}
Now what I want is to get the width of the image. For that, in my ready
event, I add these two lines:
console.log(this.$.memeImage);
console.log(this.$.memeImage.clientWidth);
The first prints the element just fine and I can see the clientWidth
and offsetWidth
are numbers. But the second line prints 0. The same is true when I use getComputedStyle()
.
I thought that this might be because the image hasn't loaded yet, so I added an event handler:
this.$.memeImage.addEventListener('onload', function() {
console.log("image loaded");
})
But this never gets hit.
What am I doing wrong and how do I fix it?