1

In this code I'm trying to change transform property on an element. When I click on element one for the first time everything works -- element translates. When I click it again nothing changes. What am I missing?

<div id="one" (click)="two.style.transform = (two.style.transform == 'translate(-100vh)') ? 'translate(0vh)' : 'translate(-100vh)'"></div>   
<div id="two" #two></div>
0

1 Answer 1

1

why this is working for first time

  • because first time assume two.style.transform == 'translate(-100vh)' is FALSE then it will run the condition translate(-100vh)

Why this is not working second time

  • because Second time condition two.style.transform == 'translate(-100vh)' is again FALSE then it will run again same condition translate(-100vh) so no effect is showing

For better clerification please run the follwing example

<div id="one" (click)="two.style.background = (two.style.background == 'grey') ? 'white' : 'red'">BB</div>   
<div id="two" #two>AA</div>
Sign up to request clarification or add additional context in comments.

2 Comments

thank you, good sir! I still can't get it to work. Is there another way of doing this inside template? I mean, toggling from one state to another
@EugeneEpifanov This might be a little late, but you can always have the conditional resolve to a class using [ngClass]. <div id="one" [ngClass]="{'translateHeight': isTranslated}"> and then have a boolean in your component to store the state isTranslated and then in your css have the class with the transform .translateHeight { transform: translate(-100vh); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.