1.切换目录

git checkout step-12
npm start

2.效果图

这里在点击右边的缩略图时,会有一个很明显的从下向上的动画过程.

3.代码实现:

step11和step12之间的代码差异:https://github.com/angular/angular-phonecat/compare/step-11...step-12

Dependencies(依赖的js库):

bower.json

{
"name": "angular-seed",
"description": "A starter project for AngularJS",
"version": "0.0.0",
"homepage": "https://github.com/angular/angular-seed",
"license": "MIT",
"private": true,
"dependencies": {
"angular": "1.2.x",
"angular-mocks": "~1.2.x",
"bootstrap": "~3.1.1",
"angular-route": "~1.2.x",
"angular-resource": "~1.2.x",
"jquery": "1.10.2",
"angular-animate": "~1.2.x"
}
}

注:angular-animate需要单独下载,这里使用命令npm install或者 bower install即可下载 angular-animate.js

如果需要更多动画可以结合Jquery中的动画去实现需求.

angularjs中是如何实现动画的? 可以参考API:https://docs.angularjs.org/guide/animations

Template(模板)

首先,引入angular-animate.js文件,如下:

...
<!-- for CSS Transitions and/or Keyframe Animations -->
<link rel="stylesheet" href="css/animations.css">
...
<!-- jQuery is used for JavaScript animations (include this before angular.js) -->
<script src="bower_components/jquery/jquery.js"></script>
...
<!-- required module to enable animation support in AngularJS -->
<script src="bower_components/angular-animate/angular-animate.js"></script> <!-- for JavaScript Animations -->
<script src="js/animations.js"></script> ...

其API可以参考:ngAnimate

Module & Animations(组件和动画)

app/js/animations.js.

angular.module('phonecatAnimations', ['ngAnimate']);
// ...
// this module will later be used to define animations
// ...

app/js/app.js

// ...
angular.module('phonecatApp', [
'ngRoute', 'phonecatAnimations',
'phonecatControllers',
'phonecatFilters',
'phonecatServices',
]);
// ...

现在,动画效果已经被唤醒了.

Animating ngRepeat with CSS Transition Animations(使用CSS过渡效果去实现动画效果)

<ul class="phones">
<li ng-repeat="phone in phones | filter:query | orderBy:orderProp"
class="thumbnail phone-listing">
<a href="#/phones/{{phone.id}}" class="thumb"><img ng-src="{{phone.imageUrl}}"></a>
<a href="#/phones/{{phone.id}}">{{phone.name}}</a>
<p>{{phone.snippet}}</p>
</li>
</ul>

app/css/animations.css

.phone-listing.ng-enter,
.phone-listing.ng-leave,
.phone-listing.ng-move {
-webkit-transition: 0.5s linear all;
-moz-transition: 0.5s linear all;
-o-transition: 0.5s linear all;
transition: 0.5s linear all;
} .phone-listing.ng-enter,
.phone-listing.ng-move {
opacity:;
height:;
overflow: hidden;
} .phone-listing.ng-move.ng-move-active,
.phone-listing.ng-enter.ng-enter-active {
opacity:;
height: 120px;
} .phone-listing.ng-leave {
opacity:;
overflow: hidden;
} .phone-listing.ng-leave.ng-leave-active {
opacity:;
height:;
padding-top:;
padding-bottom:;
}

这里都是通过class去定位元素的,那么class是何时被创建的?

  • ng-enter  class 主要用于新添加的手机并渲染到页面中.
  • ng-move  class 主要用于当元素被移动时.
  • ng-leave  class主要用于被删除时.

手机列表被添加和删除依赖于ng-repeat指令,例如,如果过滤数据时,手机列表会动态的出现列表中.

  1. starting class表示一个将要开始的动画
  2. active class 表示一个将要结束的动画

在我们上面的例子中,元素的高度变化从0到120像素当手机被添加和移除时,同样有一个淡入淡出的效果,所有这些效果都是CSS transitions (CSS 转换器)实现的.CSS transitions 和 CSS animations对于目前主流的浏览器绝大部分都有着很好的支持,除了IE 9及其更低版本,如果想要一些动画效果可以尝试基本Javascript的动画.

ngView

app/index.html.

<div class="view-container">
<div ng-view class="view-frame"></div>
</div>

With this change, the ng-view directive is nested inside a parent element with a view-container CSS class. This class adds aposition: relative style so that the positioning of the ng-view is relative to this parent as it animates transitions.

这里使用ng-view代替ng-repeat,在这里,ng-view指令被嵌套入一个view-container CSS类,这个类(class)添加了一个相对路径以便其动画效果可以动态显现.下面将看其动画的具体实现:

app/css/animations.css.

.view-container {
position: relative;
} .view-frame.ng-enter, .view-frame.ng-leave {
background: white;
position: absolute;
top:;
left:;
right:;
} .view-frame.ng-enter {
-webkit-animation: 0.5s fade-in;
-moz-animation: 0.5s fade-in;
-o-animation: 0.5s fade-in;
animation: 0.5s fade-in;
z-index:;
} .view-frame.ng-leave {
-webkit-animation: 0.5s fade-out;
-moz-animation: 0.5s fade-out;
-o-animation: 0.5s fade-out;
animation: 0.5s fade-out;
z-index:;
} @keyframes fade-in {
from { opacity:; }
to { opacity:; }
}
@-moz-keyframes fade-in {
from { opacity:; }
to { opacity:; }
}
@-webkit-keyframes fade-in {
from { opacity:; }
to { opacity:; }
} @keyframes fade-out {
from { opacity:; }
to { opacity:; }
}
@-moz-keyframes fade-out {
from { opacity:; }
to { opacity:; }
}
@-webkit-keyframes fade-out {
from { opacity:; }
to { opacity:; }
}

使用ngClass结合Javascript来实现动画效果

app/partials/phone-detail.html

<div class="phone-images">
<img ng-src="{{img}}"
class="phone"
ng-repeat="img in phone.images"
ng-class="{active:mainImageUrl==img}">
</div> <h1>{{phone.name}}</h1> <p>{{phone.description}}</p> <ul class="phone-thumbs">
<li ng-repeat="img in phone.images">
<img ng-src="{{img}}" ng-mouseenter="setImage(img)">
</li>
</ul>

动画的原理是在于"动",主要是位置或形态的改变产生的动的画面的过程.

其CSS样式如下:
app/css/app.css

.phone-images {
background-color: white;
width: 450px;
height: 450px;
overflow: hidden;
position: relative;
float: left;
} ... img.phone {
float: left;
margin-right: 3em;
margin-bottom: 2em;
background-color: white;
padding: 2em;
height: 400px;
width: 400px;
display: none;
} img.phone:first-child {
display: block;
}

这里主要用的是Jquery里的动画实现的,可以查看其API 查看更多动画: jQuery documentation.

AngularJS学习--- 动画操作 (Applying Animations) ngAnimate step 12的更多相关文章

  1. 动画操作 (Applying Animations) ngAnimate12

    动画操作 (Applying Animations) ngAnimate step 12 1.切换目录 git checkout step-12 npm start 2.效果图 这里在点击右边的缩略图 ...

  2. AngularJs学习笔记--directive

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/directive Directive是教HTML玩一些新把戏的途径.在DOM编译期间,directiv ...

  3. 【总结整理】JQuery基础学习---动画

    jQuery中隐藏元素的hide方法 让页面上的元素不可见,一般可以通过设置css的display为none属性.但是通过css直接修改是静态的布局,如果在代码执行的时候,一般是通过js控制元素的st ...

  4. AngularJs学习总结-了解基本特性(-)

    现在的前端项目中基本上都会用到angularjs框架,之前并不了解这个框架,也是因为最近接手的项目,所以打算好好的学习下它.之前都是搞pc端,现在接手的是移动端的项目,移动端UI框架用的是ionic+ ...

  5. [整理]AngularJS学习资源

    https://angular.io/docs/js/latest/(2.0官方网站) http://www.linuxidc.com/Linux/2014-05/102139.htm(Angular ...

  6. 【AngularJS学习笔记】02 小杂烩及学习总结

    表格示例 <div ng-app="myApp" ng-controller="customersCtrl"> <table> < ...

  7. AngularJs学习笔记--bootstrap

    AngularJs学习笔记系列第一篇,希望我可以坚持写下去.本文内容主要来自 http://docs.angularjs.org/guide/ 文档的内容,但也加入些许自己的理解与尝试结果. 一.总括 ...

  8. AngularJs学习笔记--concepts(概念)

    原版地址:http://code.angularjs.org/1.0.2/docs/guide/concepts 继续.. 一.总括 本文主要是angular组件(components)的概览,并说明 ...

  9. AngularJS学习笔记2——AngularJS的初始化

    本文主要介绍AngularJS的自动初始化以及在必要的适合如何手动初始化. Angular <script> Tag 下面通过一小段代码来介绍推荐的自动初始化过程: <!doctyp ...

随机推荐

  1. 基于Python的接口测试框架

    分析 接口是基于HTTP协议的,那么说白了,就是发起HTTP请求就行了,对于Python来说比较简单.直接使用requests就可以很轻松的完成任务. 架构 整个框架是比较小的,涉及的东西也比较少,只 ...

  2. Spring中的ApplicationContext事件机制

    ApplicationContext的事件机制是观察者设计模式的实现,通过ApplicationEvent类和ApplicationListerner接口来实现. 1. 创建EmailEvent pu ...

  3. 揭开Sass和Compass的神秘面纱

    揭开Sass和Compass的神秘面纱 可能之前你像我一样,对Sass和Compass毫无所知,好一点儿的可能知道它们是用来作为CSS预处理的.那么,今天请跟我一起学习下Sass和Compass的一些 ...

  4. FCKeditor 2.6.4.1 初始化值不能显示中文问题

    最近在学习PHP加入FCKeditor 在线编辑器,发现在初始化Value赋值时,英文可以显示出来,中文怎么都显示不出来,连乱码都不显示. 经过漫长的探索,终于找到原因了! 在fckeditor目录下 ...

  5. Linux下date命令,格式化输出,时间设置

    date命令的帮助信息 [root@localhost source]# date --help用法:date [选项]... [+格式] 或:date [-u|--utc|--universal] ...

  6. python 数据库 blob类型 转字符串

    例如: 从数据库里读出了blob类型,如 z = b'61736467' 在py里转化成字符串:bytes.fromhex(z).decode('utf8')

  7. restfull api

    REST 表示状态传输.这是一个体系结构样式,可用于设计网络服务,可以被各种客户端消耗.核心思想是,不使用如CORBA,RPC或SOAP复杂的机制在机器之间进行连接,简单的 HTTP 用于使它们之间调 ...

  8. 慕课网-安卓工程师初养成-6-5 使用循环操作 Java 中的数组

    来源:http://www.imooc.com/code/1531 实际开发中我们经常使用循环控制数组成员的操作.如: 运行结果: 其中,  用于获取数组的长度 需要注意的“小毛病”: 1. 数组下标 ...

  9. Statement及PreparedStatement执行多个sql

        这两个对象的区别: 1.Statement它更适合执行不同sql的批处理,它没有提供预处理功能,性能比较低. 2.PreparedStatement它适合执行相同的批处理,它提供了预处理功能, ...

  10. 纸上谈兵:排序算法简介及C实现

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 排序算法(Sorting Algorithm)是计算机算法的一个组成部分. 排序的 ...