我们假设我们有一个单页面的程序,并且想为这个页面添加动画效果。点击某一个链接会将一个试图滑出,同时将另一个试图滑入。

我们将会使用:

  • 使用 ngRoute 来为我们的页面路由

  • 使用 ngAnimate 来为页面创建动画效果

  • 对页面使用 CSS Animations

  • 当我们离开或进入试图时,我们的每一个页面会有不同的动画效果

让我们看一下ngAnimate是如何工作的。ngAnimate 会根据是进入还是离开视图来为不同的Angular 指令(directive)添加和移除不同的CSS类名。例如,当我们加载网站时,无论ng-view中填充了什么都会得到一个.ng-enter的类名。

我们所需要做的就是给.ng-enter 类名添加CSS动画效果,该动画在进入的时候会自动生效。

ngAnimate 可以应用于: ngRepeat, ngInclude, ngIf, ngSwitch, ngShow, ngHide, ngView 以及 ngClass

需要的文件列表:

- index.html
- style.css
- app.js
- page-home.html
- page-about.html
- page-contact.html index.html,
加载我们需要的资源,定义我们的Angular app,并且为我们注入的视图添加ng-view类名。
<html>
<head> <!-- CSS -->
<!-- load bootstrap (bootswatch version) -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootswatch/3.1.1/readable/bootstrap.min.css">
<link rel="stylesheet" href="style.css"> <!-- JS -->
<!-- load angular, ngRoute, ngAnimate -->
<script type="text/javascript" src="http://apps.bdimg.com/libs/angular.js/1.2.13/angular.min.js"></script>
<script src="http://lib.baomitu.com/angular.js/1.2.13/angular-route.min.js"></script>
<script src="http://lib.baomitu.com/angular.js/1.2.13/angular-animate.min.js"></script>
<script src="app.js"></script> </head> <!-- apply our angular app -->
<body ng-app="animateApp"> <!-- inject our views using ng-view -->
<!-- each angular controller applies a different class here -->
<div class="page {{ pageClass }}" ng-view></div> </body>
</html>

app.js

现在,我们需要将会创建一个带路由的Angular程序,以便我们可以在不刷新页面的情况下修改我们的页面。

// define our application and pull in ngRoute and ngAnimate
var animateApp = angular.module('animateApp', ['ngRoute', 'ngAnimate']); // ROUTING ===============================================
// set our routing for this application
// each route will pull in a different controller
animateApp.config(function($routeProvider) { $routeProvider // home page
.when('/', {
templateUrl: 'page-home.html',
controller: 'mainController'
}) // about page
.when('/about', {
templateUrl: 'page-about.html',
controller: 'aboutController'
}) // contact page
.when('/contact', {
templateUrl: 'page-contact.html',
controller: 'contactController'
}); }); // CONTROLLERS ============================================
// home page controller
animateApp.controller('mainController', function($scope) {
$scope.pageClass = 'page-home';
}); // about page controller
animateApp.controller('aboutController', function($scope) {
$scope.pageClass = 'page-about';
}); // contact page controller
animateApp.controller('contactController', function($scope) {
$scope.pageClass = 'page-contact';
});

page-home.html

<!-- page-home.html -->
<h1>Angular Animations Shenanigans</h1>
<h2>Home</h2> <a href="#about" class="btn btn-success btn-lg">About</a>
<a href="#contact" class="btn btn-danger btn-lg">Contact</a>

page-about.html

<!-- page-about.html -->
<h1>Animating Pages Is Fun</h1>
<h2>About</h2> <a href="#" class="btn btn-primary btn-lg">Home</a>
<a href="#contact" class="btn btn-danger btn-lg">Contact</a>

page-contact.html

<!-- page-contact.html -->
<h1>Tons of Animations</h1>
<h2>Contact</h2> <a href="#" class="btn btn-primary btn-lg">Home</a>
<a href="#about" class="btn btn-success btn-lg">About</a>

style.css

/* make our pages be full width and full height */

/* positioned absolutely so that the pages can overlap each other as they enter and leave */

.page {
bottom:;
padding-top: 200px;
position: absolute;
text-align: center;
top:;
width: 100%;
} .page h1 {
font-size: 60px;
} .page a {
margin-top: 50px;
} /* PAGES (specific colors for each page)
============================================================================= */ .page-home {
background: #00D0BC;
color: #00907c;
} .page-about {
background: #E59400;
color: #a55400;
} .page-contact {
background: #ffa6bb;
color: #9e0000;
} /* ANIMATIONS
============================================================================= */ .ng-enter {
animation: scaleUp 0.5s both ease-in;
z-index:;
} .ng-leave {
animation: slideOutLeft 0.5s both ease-in;
z-index:;
} .ng-enter {
z-index:;
} .ng-leave {
z-index:;
} /* page specific animations ------------------------ */ /* home -------------------------- */ .page-home.ng-enter {
animation: scaleUp 0.5s both ease-in;
} .page-home.ng-leave {
transform-origin: 0% 0%;
animation: rotateFall 1s both ease-in;
} /* about ------------------------ */ .page-about.ng-enter {
animation: slideInRight 0.5s both ease-in;
} .page-about.ng-leave {
animation: slideOutLeft 0.5s both ease-in;
} /* contact ---------------------- */ .page-contact.ng-leave {
transform-origin: 50% 50%;
animation: rotateOutNewspaper .5s both ease-in;
} .page-contact.ng-enter {
animation: slideInUp 0.5s both ease-in;
} /* leaving animations ----------------------------------------- */ /* rotate and fall */ @keyframes rotateFall {
0% {
transform: rotateZ(0deg);
}
20% {
transform: rotateZ(10deg);
animation-timing-function: ease-out;
}
40% {
transform: rotateZ(17deg);
}
60% {
transform: rotateZ(16deg);
}
100% {
transform: translateY(100%) rotateZ(17deg);
}
} /* slide in from the bottom */ @keyframes slideOutLeft {
to {
transform: translateX(-100%);
}
} /* rotate out newspaper */ @keyframes rotateOutNewspaper {
to {
transform: translateZ(-3000px) rotateZ(360deg);
opacity:;
}
} /* entering animations --------------------------------------- */ /* scale up */ @keyframes scaleUp {
from {
opacity: 0.3;
-webkit-transform: scale(0.8);
}
} /* slide in from the right */ @keyframes slideInRight {
from {
transform: translateX(100%);
}
to {
transform: translateX(0);
}
} /* slide in from the bottom */ @keyframes slideInUp {
from {
transform: translateY(100%);
}
to {
transform: translateY(0);
}
}

我们将使用CSS来添加所有的动画效果。这种方法很棒,因为我们所要做的事就是添加ngAnimate,并且不用修改我们的代码就可以添加CSS动画效果。

ngAnimate为我们的ng-view添加的两个类分别是.ng-enter和.ng-leave。你可以想象一些他们各自的作用。

我们定义了6个不同的动画效果。每一个页面都会有他们各自的ng-enter 和 ng-leave 的动画效果。

用ng-view创建单页APP的更多相关文章

  1. 七天学会ASP.NET MVC(七)——创建单页应用

    系列文章 七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC 七天学会ASP.NET MVC (二)——ASP.NET MVC 数据传递 七天学会ASP.NET MVC (三)— ...

  2. 七天学会ASP.NET MVC(七)——创建单页应用 【转】

    http://www.cnblogs.com/powertoolsteam/p/MVC_Seven.html 系列文章 七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC 七天学 ...

  3. Nodejs之MEAN栈开发(六)---- 用Angular创建单页应用(上)

    在上一节中我们学会了如何在页面中添加一个组件以及一些基本的Angular知识,而这一节将用Angular来创建一个单页应用(SPA).这意味着,取代我们之前用Express在服务端运行整个网站逻辑的方 ...

  4. 创建单页web app, 如何在chrome中隐藏工具栏 地址栏 标签栏?

    问题描述: 为使用更大的屏幕空间,在访问web应用的使用,如何隐藏地址栏.工具栏? 解决办法: 1. chrome的application mode 选项--->更多工具---->添加到桌 ...

  5. Nodejs之MEAN栈开发(七)---- 用Angular创建单页应用(下)

    上一节我们走通了基本的SPA基础结构,这一节会更彻底的将后端的视图.路由.控制器全部移到前端.篇幅比较长,主要分页面改造.使用AngularUI两大部分以及一些优化路由.使用Angular的其他指令的 ...

  6. vue学习之用 Vue.js + Vue Router 创建单页应用的几个步骤

    通过vue学习一:新建或打开vue项目,创建好项目后,接下来的操作为: src目录重新规划——>新建几个页面——>配置这几个页面的路由——>给根实例注入路由配置 src目录重整 在项 ...

  7. 【ASP.NET MVC 5】第27章 Web API与单页应用程序

    注:<精通ASP.NET MVC 3框架>受到了出版社和广大读者的充分肯定,这让本人深感欣慰.目前该书的第4版不日即将出版,现在又已开始第5版的翻译,这里先贴出该书的最后一章译稿,仅供大家 ...

  8. 高效开发 Web 单页应用解决方案

    于 2017 年初,有在 Github 建立并维护一个项目:Vue Boilerplate Template,欲成就一款开箱即用 Vue + Webpack 的脚手架模版:其目标与宗旨是:根据以往经验 ...

  9. Singal Page App:使用Knockout和RequireJS创建高度模块化的单页应用引擎

    Singal Page App 开篇扯淡 距离上一篇文章已经有好几个月,也不是没有时间记录点东西,主要是换了新的工作,在一家外资工作,目前的工作内容大多都是前端开发,新接触的东西因为时间原因,大多还不 ...

随机推荐

  1. Layoutinlater 转

    http://blog.csdn.net/guolin_blog/article/details/12921889

  2. Android时光轴

    时间轴,顾名思义就是将一些事件或者事物等按照时间顺序罗列起来,给用户带来一种更加直观的体验.京东和淘宝等的物流顺序就是一个时间轴 前言:​Android中使用RecyclerView实现时光轴,代码简 ...

  3. 开启Tomcat远程调试(转)

    原文链接:http://www.07net01.com/2016/11/1721293.html 如何远程调试tomcat 一,linux环境下 1. 服防火墙打开8000端口,允许外网访问:2. 修 ...

  4. 疑问:Spring中构造器、init-method、@PostConstruct、afterPropertiesSet孰先孰后,自动注入发生时间

    问题:今天想写一个通用点的方法,根据传入的参数的类型(clazz对象),判断使用哪个mapper来插入mysql数据库. 下面是我的写法: public interface BizNeeqCommon ...

  5. Oracle子查询中any、some、all之间的区别

    用some,any和all对子查询中返回的多行结果进行处理. 下面我们来简单介一下这几个关键词的含义. * Some在此表示满足其中一个的意义,是用or串起来的比较从句. * Any也表示满足其中一个 ...

  6. Python之路-操作系统&网络基础

    一.为何要有操作系统 没有操作系统的话,计算机同样可以运行,但是程序员要了解到计算机底层各种各样的细节,而操作系统聪明地封装起来了底层这些繁杂的操作,通过向程序员开放一个个的接口,来最终使我们实现对底 ...

  7. android Instrumentoation 问答

    android Instrumentoation 问答   1.instrumentation是执行application instrumentation代码的基类.当应用程序运行的时候instrum ...

  8. JavaScript 数组操作方法

    这些数组的操作方法会改变原来的数组.在使用 Vue 或者 Angular 等框架的时候会非常实用,使用这些方法修改数组会触发视图的更新. Array.prototype.push 该方法可以在数组末尾 ...

  9. 【转载】关于c++中的explicit

    按照默认规定,只有一个参数的构造函数也定义了一个隐式转换,将该构造函数对应数据类型的数据转换为该类对象,如下面所示: class String { String ( const char* p );  ...

  10. kafka入门

    1.Kafka独特设计在什么地方?2.Kafka如何搭建及创建topic.发送消息.消费消息?3.如何书写Kafka程序?4.数据传输的事务定义有哪三种?5.Kafka判断一个节点是否活着有哪两个条件 ...