Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit e6e6367

Browse files
petebacondarwingkalpak
authored andcommitted
step-14 Animations
- Add animations to the application: - Animate changes to the phone list, adding, removing and reordering phones with `ngRepeat`. - Animate view transitions with `ngView`. - Animate changes to the main phone image in the phone detail view. - Showcase three different kinds of animations: - CSS transition animations. - CSS keyframe animations. - JavaScript-based animations.
1 parent 6e41f1e commit e6e6367

10 files changed

+147
-7
lines changed

app/app.animations.css

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* Animate `ngRepeat` in `phoneList` component */
2+
.phone-list-item.ng-enter,
3+
.phone-list-item.ng-leave,
4+
.phone-list-item.ng-move {
5+
overflow: hidden;
6+
transition: 0.5s linear all;
7+
}
8+
9+
.phone-list-item.ng-enter,
10+
.phone-list-item.ng-leave.ng-leave-active,
11+
.phone-list-item.ng-move {
12+
height: 0;
13+
margin-bottom: 0;
14+
opacity: 0;
15+
padding-bottom: 0;
16+
padding-top: 0;
17+
}
18+
19+
.phone-list-item.ng-enter.ng-enter-active,
20+
.phone-list-item.ng-leave,
21+
.phone-list-item.ng-move.ng-move-active {
22+
height: 120px;
23+
margin-bottom: 20px;
24+
opacity: 1;
25+
padding-bottom: 4px;
26+
padding-top: 15px;
27+
}
28+
29+
/* Animate view transitions with `ngView` */
30+
.view-container {
31+
position: relative;
32+
}
33+
34+
.view-frame {
35+
margin-top: 20px;
36+
}
37+
38+
.view-frame.ng-enter,
39+
.view-frame.ng-leave {
40+
background: white;
41+
left: 0;
42+
position: absolute;
43+
right: 0;
44+
top: 0;
45+
}
46+
47+
.view-frame.ng-enter {
48+
animation: 1s fade-in;
49+
z-index: 100;
50+
}
51+
52+
.view-frame.ng-leave {
53+
animation: 1s fade-out;
54+
z-index: 99;
55+
}
56+
57+
@keyframes fade-in {
58+
from { opacity: 0; }
59+
to { opacity: 1; }
60+
}
61+
62+
@keyframes fade-out {
63+
from { opacity: 1; }
64+
to { opacity: 0; }
65+
}
66+
67+
/* Older browsers might need vendor-prefixes for keyframes and animation! */

app/app.animations.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
angular.
4+
module('phonecatApp').
5+
animation('.phone', function phoneAnimationFactory() {
6+
return {
7+
addClass: animateIn,
8+
removeClass: animateOut
9+
};
10+
11+
function animateIn(element, className, done) {
12+
if (className !== 'selected') return;
13+
14+
element.css({
15+
display: 'block',
16+
position: 'absolute',
17+
top: 500,
18+
left: 0
19+
}).animate({
20+
top: 0
21+
}, done);
22+
23+
return function animateInEnd(wasCanceled) {
24+
if (wasCanceled) element.stop();
25+
};
26+
}
27+
28+
function animateOut(element, className, done) {
29+
if (className !== 'selected') return;
30+
31+
element.css({
32+
position: 'absolute',
33+
top: 0,
34+
left: 0
35+
}).animate({
36+
top: -500
37+
}, done);
38+
39+
return function animateOutEnd(wasCanceled) {
40+
if (wasCanceled) element.stop();
41+
};
42+
}
43+
});

app/app.css

+15-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ body {
44

55
h1 {
66
border-bottom: 1px solid gray;
7+
margin-top: 0;
78
}
89

910
/* View: Phone list */
@@ -28,7 +29,7 @@ h1 {
2829
/* View: Phone detail */
2930
.phone {
3031
background-color: white;
31-
border: 1px solid black;
32+
display: none;
3233
float: left;
3334
height: 400px;
3435
margin-bottom: 2em;
@@ -37,6 +38,19 @@ h1 {
3738
width: 400px;
3839
}
3940

41+
.phone:first-child {
42+
display: block;
43+
}
44+
45+
.phone-images {
46+
background-color: white;
47+
float: left;
48+
height: 450px;
49+
overflow: hidden;
50+
position: relative;
51+
width: 450px;
52+
}
53+
4054
.phone-thumbs {
4155
list-style: none;
4256
margin: 0;

app/app.module.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// Define the `phonecatApp` module
44
angular.module('phonecatApp', [
5+
'ngAnimate',
56
'ngRoute',
67
'core',
78
'phoneDetail',

app/index.html

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
<title>Google Phone Gallery</title>
66
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" />
77
<link rel="stylesheet" href="app.css" />
8+
<link rel="stylesheet" href="app.animations.css" />
9+
10+
<script src="bower_components/jquery/dist/jquery.js"></script>
811
<script src="bower_components/angular/angular.js"></script>
12+
<script src="bower_components/angular-animate/angular-animate.js"></script>
913
<script src="bower_components/angular-resource/angular-resource.js"></script>
1014
<script src="bower_components/angular-route/angular-route.js"></script>
1115
<script src="app.module.js"></script>
1216
<script src="app.config.js"></script>
17+
<script src="app.animations.js"></script>
1318
<script src="core/core.module.js"></script>
1419
<script src="core/checkmark/checkmark.filter.js"></script>
1520
<script src="core/phone/phone.module.js"></script>
@@ -21,7 +26,9 @@
2126
</head>
2227
<body>
2328

24-
<div ng-view></div>
29+
<div class="view-container">
30+
<div ng-view class="view-frame"></div>
31+
</div>
2532

2633
</body>
2734
</html>

app/phone-detail/phone-detail.template.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
<img ng-src="{{$ctrl.mainImageUrl}}" class="phone" />
1+
<div class="phone-images">
2+
<img ng-src="{{img}}" class="phone"
3+
ng-class="{selected: img === $ctrl.mainImageUrl}"
4+
ng-repeat="img in $ctrl.phone.images" />
5+
</div>
26

37
<h1>{{$ctrl.phone.name}}</h1>
48

app/phone-list/phone-list.template.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
<!--Body content-->
2222

2323
<ul class="phones">
24-
<li ng-repeat="phone in $ctrl.phones | filter:$ctrl.query | orderBy:$ctrl.orderProp" class="thumbnail">
24+
<li ng-repeat="phone in $ctrl.phones | filter:$ctrl.query | orderBy:$ctrl.orderProp"
25+
class="thumbnail phone-list-item">
2526
<a href="#!/phones/{{phone.id}}" class="thumb">
2627
<img ng-src="{{phone.imageUrl}}" alt="{{phone.name}}" />
2728
</a>

bower.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
"private": true,
88
"dependencies": {
99
"angular": "1.5.x",
10+
"angular-animate": "1.5.x",
1011
"angular-mocks": "1.5.x",
1112
"angular-resource": "1.5.x",
1213
"angular-route": "1.5.x",
13-
"bootstrap": "3.3.x"
14+
"bootstrap": "3.3.x",
15+
"jquery": "2.2.x"
1416
}
1517
}

e2e-tests/scenarios.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ describe('PhoneCat Application', function() {
7878
});
7979

8080
it('should display the first phone image as the main phone image', function() {
81-
var mainImage = element(by.css('img.phone'));
81+
var mainImage = element(by.css('img.phone.selected'));
8282

8383
expect(mainImage.getAttribute('src')).toMatch(/img\/phones\/nexus-s.0.jpg/);
8484
});
8585

8686
it('should swap the main image when clicking on a thumbnail image', function() {
87-
var mainImage = element(by.css('img.phone'));
87+
var mainImage = element(by.css('img.phone.selected'));
8888
var thumbnails = element.all(by.css('.phone-thumbs img'));
8989

9090
thumbnails.get(2).click();

karma.conf.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = function(config) {
66

77
files: [
88
'bower_components/angular/angular.js',
9+
'bower_components/angular-animate/angular-animate.js',
910
'bower_components/angular-resource/angular-resource.js',
1011
'bower_components/angular-route/angular-route.js',
1112
'bower_components/angular-mocks/angular-mocks.js',

0 commit comments

Comments
 (0)