subZero
subZero

Reputation: 5186

Prevent animation from firing on inital page load

I have created a simple rotating cube animation on the ng-view directive using ngAnimate with Angular 1.2 and have this CSS:

.cube-container {
  -webkit-transform-style: preserve-3d;
  -webkit-perspective:400px;
  height:100%;
}
.cube.ng-enter, 
.cube.ng-leave { 
  -webkit-transition: 0.8s linear all;
}
.cube.ng-enter {
  -webkit-transform-origin: 100% 50%;  
  -webkit-transform: translateX(-100%) rotateY(-90deg);
}
.cube.ng-enter.ng-enter-active {
  -webkit-transform: translateX(0%) rotateY(0deg);
}

.cube.ng-leave {
  -webkit-transform-origin: 0% 50%;
}
.cube.ng-leave.ng-leave-active {
    -webkit-transform: translateX(100%) rotateY(90deg);
}

The markup looks like this:

<div class="cube-container">
    <div class="app cube" ng-view></div>
</div>

This works absolutely great. The problem is: how can I stop the animation from firing on the initial first page load, and only apply when the route changes?

Thanks!

Upvotes: 4

Views: 2092

Answers (2)

raykin
raykin

Reputation: 1757

Here is my solution. Setup a class after page init several seconds then add this class on all animation css selector. the js sample code:

myAppModule.run(function($rootScope, $timeout) {
  $timeout(function() {
    $rootScope.pageInited = true;
  }, 5000)
});

the html code:

<div class="cube-container">
  <div class="app cube" ng-view ng-class="{'page-inited': pageInited}"></div>
</div>

you can add the ng-class on specific tag that need to be animate.

then css code, only add on animation css:

.page-inited.cube.ng-enter, 
.page-inited.cube.ng-leave { 
  -webkit-transition: 0.8s linear all;
}

Upvotes: 1

Nader Chehab
Nader Chehab

Reputation: 255

You need to apply the animation class dynamically as shown here:

http://jsfiddle.net/J63vD/

I believe this can be done on any event such as the route change event.

HTML:

<div ng-app="App">
    <input type="button" value="set" ng-click="myCssVar='css-class'" />
    <input type="button" value="clear" ng-click="myCssVar=''" />
    <span ng-class="myCssVar">CSS-Animated Text</span>
</div>

CSS:

.css-class-add, .css-class-remove {
  -webkit-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
  -moz-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
  -o-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
  transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s;
}

.css-class,
.css-class-add.css-class-add-active {
  color: red;
  font-size:3em;
}

.css-class-remove.css-class-remove-active {
  font-size:1.0em;
  color:black;
}

JavaScript:

angular.module('App', ['ngAnimate']);

Upvotes: 1

Related Questions