3

I'm trying to fade in/out a div on click of a link using ngAnimate and css3 transitions. I have the following, but it isn't working. The div is shown/hidden, but does not fade in or out. Where did I go wrong:

.fade-in-out.ng-add {
  transition: 1s linear all;
  opacity: 0;
}

.fade-in-out.ng-add-active {
  opacity: 1;
}

.fade-in-out.ng-remove {
  transition: 1s linear all;
  opacity: 1;
}

.fade-in-out.ng-remove-active {
  opacity: 0;
}

The div is initially hidden (showMe=false). On the page is a link which sets showMe to true.

<div ng-show="showMe" class="fade-in-out">          
    <div style="float: right; cursor: pointer;" ng-click="showMe=false">x</div>
    blablabla
</div>

Note that I'm using angular 1.2.26.

1 Answer 1

17

The correct classes to use are:

.my-element.ng-hide-add, .my-element.ng-hide-remove {
  transition:0.5s linear all;
}

.my-element.ng-hide-add { ... }
.my-element.ng-hide-add.ng-hide-add-active { ... }
.my-element.ng-hide-remove { ... }
.my-element.ng-hide-remove.ng-hide-remove-active { ... }

In your case this will be enough:

.fade-in-out {
  transition: 1s linear all;  
  opacity: 1;
}

.fade-in-out.ng-hide {
  opacity: 0;
}

Demo: http://plnkr.co/edit/WM60wEYeQD7J1GuHG0WL?p=preview

Note that angular-animate.js must be loaded and ngAnimate must be added as a dependant module.

Sign up to request clarification or add additional context in comments.

5 Comments

Just for understanding, why would ng-show not be the correct class to use?
Angular adds ng-hide when hiding something and removes it when showing something. Using the ng-show or ng-hide directive doesn't matter. There is no ng-show class that is added by Angular.
Since you're well versed in this area, can you take a look at a similar issue: stackoverflow.com/questions/27140770/…
Sure, will take a look :)
Thank you for your footer note, helped me avoid a giant headache!

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.