2

I have an div in which I show some data:

div.movie_details
    div.movie_details_container
        span {{ movieDetail.title }}
        span {{ movieDetail.overview }}

I also have a button that changes the information that's been displayed:

ng-click="toggleRow(movie)

The toggleRow function:

$scope.toggleRow = function(movie){

    $scope.movieDetail = movie;
};

So when a user clicks on the button the toggleRow function fires and sends the movie object as a parameter to the function. The function then changes the scope which changes the information displayed in the view. Works fine.

My problem is that I want to make a transition animation when the information in the div.movie_details_container changes. But I can't figure the correct order of actions.

I made a plunkr with a simplified version of the project: https://plnkr.co/edit/wlmVaxVhax0b07cvXXld?p=preview So when the user clicks on a title the change is very abrupt. I would like a nice fade in/out transition.

2
  • What kind of a transition/animation do you want to do? Commented Aug 12, 2016 at 12:26
  • That doesn't really matter much. With CSS I can do anything I want but I can't figure out how to set it up. But I would like the current info to fade out and then fade in the new info. Commented Aug 12, 2016 at 12:27

2 Answers 2

1

You could just use css animations to do what you want. Angular adds the ng-hide class to the divs hidden with ng-show or ng-hide and you can take advantage of that. Here's a simple and very minimalistic demo:

CSS:

.group {
  border: 1px solid green;
  margin-bottom: 5px;
  padding: 5px;
}

.detail {
  border: 1px solid black;
  padding: 5px;
  animation: 0.5s fadeInAnimation ease;
}

.detail.ng-hide {
  height: 0;
  opacity: 0;
}

@keyframes fadeInAnimation {
  0% {
    height: 0;
    opacity: 0;
    display: none;
  }

  100% {
    height: auto;
    opacity: 1;
    display: block;
  }
}

Plunker demo

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

4 Comments

I would also add overflow-y: hidden; to .detail and height: 18px at 100% animation step (auto height makes it run stairway)
Yep you are right. This example was just to point out a way to do it in it's most simple format :)
Maybe I didn't explain it well enough in my question, but I wanted to animate the transition between movie info when you click on the title. So you have a fade in/out on the X {"title":"Star Wars 1"} and X {"title":"Star Wars 2"} I managed fixing this using css transition delays.
Ok, well I'm glad you got it fixed anyways :)
1

You can use ngAnimate for that:

  1. Include angular-animate.js in the file:

    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular-animate.js"></script>
    

angular-animate.js should be below angular.min.js.

  1. Load the module in your application by adding it as a dependent module:

    angular.module('app', ['ngAnimate']);
    
  2. Add styling:

    .detail{
        border: 1px solid black;
        padding: 5px;
        transition: all linear 0.5s;
    }
    .detail.ng-hide {
        opacity: 0;
    }
    

And it's working: https://plnkr.co/edit/qrS8EBg8jitcqBqT3TqW?p=preview

Reading more about ngAnimate: https://docs.angularjs.org/api/ngAnimate

1 Comment

Maybe I didn't explain it well enough in my question, but I wanted to animate the transition between movie info when you click on the title. So you have a fade in/out on the X {"title":"Star Wars 1"} and X {"title":"Star Wars 2"} I managed fixing this using css transition delays.

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.