Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 change position of element

Tags:

angular

In a component template I have an image. Dependent on a boolean I want to place another image on top of it. The end result looks something like this: enter image description here

The second image is the green selector image. In order to do so, I need to place the selector image dependent on the dimensions of the main title image. ETA: Some images are higher than wide whereas others are wider then high. The top offset of the selector image is therefor dependent on the actual height of the title image.

I have an event handler for the load event of the images and when both are loaded I want to do the positioning. Both images are available in the component via @ViewChild. I tried to set the offsetTop and offsetLeft properties of the nativeElement but they are read-only. I have verified that I have both images in my load handler with the correct dimensions.

How can I do that in either the template itself or in my load handler?

like image 837
hholtij Avatar asked Jun 18 '16 14:06

hholtij


1 Answers

In case somebody else needs a solution for a similar problem:

I compute top and left position (based on the title image) in the load handler and in the template on the select image I added something like [style.top]="top" [style.left]="left" (where top and left are strings ending with "px").

like image 147
hholtij Avatar answered Nov 13 '22 22:11

hholtij