AngularJS 天生適合做單頁應用,而單頁應用看起來「像原生 App」的關鍵之一,是頁面切換有過渡動畫。ngAnimate 模塊把這件事簡化到只需寫 CSS。本文完整走一遍:路由、視圖、控制器,再到六種進出動畫。
一個帶首頁、關於、聯繫我們三頁的單頁應用。點擊鏈接時,舊頁面滑出、新頁面滑入,且每個頁面可以有自己的動畫風格。
技術清單:
ngRoute 做頁面路由;ngAnimate 提供動畫能力;ngAnimate 的機制非常簡單:當某個 Angular 指令導致元素進入或離開時,它會自動為元素添加或移除特定的 CSS 類名。
例如頁面加載時,ng-view 裡注入的內容會被加上 .ng-enter 類;離開時則是 .ng-leave。你要做的只是給這些類寫上 CSS 動畫,剩下的全由 Angular 完成。
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 -->
<!DOCTYPE html>
<html>
<head>
<!-- CSS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootswatch/3.1.1/readable/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
<!-- JS:加載 angular、ngRoute、ngAnimate -->
<script src="https://code.angularjs.org/1.2.13/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular-route.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular-animate.js"></script>
<script src="app.js"></script>
</head>
<!-- 應用我們的 angular app -->
<body ng-app="animateApp">
<!-- 用 ng-view 注入視圖,pageClass 由控制器動態提供 -->
<div class="page {{ pageClass }}" ng-view></div>
</body>
</html>
注意 class="page {{ pageClass }}" 這個細節:每個控制器會給 pageClass 賦不同的值,於是每個頁面渲染時都帶上自己的類名,這正是後面能為不同頁面定制動畫的基礎。
// app.js
// 定義應用並引入 ngRoute 與 ngAnimate
var animateApp = angular.module('animateApp', ['ngRoute', 'ngAnimate']);
// 路由配置
animateApp.config(function ($routeProvider) {
$routeProvider
// 首頁
.when('/', {
templateUrl: 'page-home.html',
controller: 'mainController'
})
// 關於頁
.when('/about', {
templateUrl: 'page-about.html',
controller: 'aboutController'
})
// 聯繫我們
.when('/contact', {
templateUrl: 'page-contact.html',
controller: 'contactController'
});
});
// 控制器:各自設置不同的 pageClass
animateApp.controller('mainController', function ($scope) {
$scope.pageClass = 'page-home';
});
animateApp.controller('aboutController', function ($scope) {
$scope.pageClass = 'page-about';
});
animateApp.controller('contactController', function ($scope) {
$scope.pageClass = 'page-contact';
});
<!-- 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 --> <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 --> <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>
到這一步,應用已經可以正常切換頁面,只是還沒有動畫。
/* 讓每頁鋪滿全屏,並用絕對定位讓進出頁面可以重疊 */
.page {
bottom: 0;
padding-top: 200px;
position: absolute;
text-align: center;
top: 0;
width: 100%;
}
.page h1 { font-size: 60px; }
.page a { margin-top: 50px; }
/* 各頁配色 */
.page-home { background: #00D0BC; color: #00907c; }
.page-about { background: #E59400; color: #a55400; }
.page-contact { background: #ffa6bb; color: #9e0000; }
position: absolute 是這裡的關鍵:只有讓頁面脫離文檔流並絕對定位,離開的頁面與進入的頁面才能同時存在於視區內,形成重疊過渡的效果。
/* ANIMATIONS */
/* 離開動畫 */
/* 旋轉並墜落 */
@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); }
}
/* 向左滑出 */
@keyframes slideOutLeft {
to { transform: translateX(-100%); }
}
/* 報紙式旋轉離場 */
@keyframes rotateOutNewspaper {
to { transform: translateZ(-3000px) rotateZ(360deg); opacity: 0; }
}
/* 進入動畫 */
/* 放大浮現 */
@keyframes scaleUp {
from { opacity: 0.3; transform: scale(0.8); }
}
/* 從右側滑入 */
@keyframes slideInRight {
from { transform: translateX(100%); }
to { transform: translateX(0); }
}
/* 從底部滑入 */
@keyframes slideInUp {
from { transform: translateY(100%); }
to { transform: translateY(0); }
}
完整的生產代碼還應補上 -webkit-、-moz- 等前綴,以兼容較老的瀏覽器版本。
.ng-enter {
animation: scaleUp 0.5s both ease-in;
z-index: 8888;
}
.ng-leave {
animation: slideOutLeft 0.5s both ease-in;
z-index: 9999;
}
此時所有頁面都是同一套效果:進入時放大浮現,離開時向左滑出。z-index 的作用是把離開的頁面置於進入頁之上,避免新頁面從舊頁面「背後鑽出」的突兀感。
利用前面提到的 pageClass,把選擇器寫得更具體,就能讓每頁各具風格:
.ng-enter { z-index: 8888; }
.ng-leave { z-index: 9999; }
/* 首頁:放大進入,旋轉墜落離開 */
.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;
}
/* 關於頁:左右滑動 */
.page-about.ng-enter { animation: slideInRight 0.5s both ease-in; }
.page-about.ng-leave { animation: slideOutLeft 0.5s both ease-in; }
/* 聯繫頁:報紙式旋轉離開,底部滑入 */
.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; }
整個方案的實現成本極低:引入 ngAnimate、寫 CSS 動畫、按需掛類,業務代碼一行都不用改。這正是 Angular 聲明式設計的價值所在。
實戰中還有幾點值得注意:
@media (prefers-reduced-motion: reduce) 判斷,為關閉動效的用戶禁用動畫,這是無障礙的基礎要求。transform 與 opacity,它們能觸發 GPU 合成,避免觸發重排;rotateOutNewspaper 這類 3D 變換在低配設備上要多測一測。file:// 打開會因跨域限制失敗,請通過 https://localhost 訪問。