AngularJS 動畫
AngularJS 提供了動畫效果,可以配合 CSS 使用。
AngularJS 使用動畫需要引入 angular-animate.min.js 庫。
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular-animate.min.js"></script>
還需在應用中使用模型 ngAnimate:
<body ng-app="ngAnimate">
什么是動畫?
動畫是通過改變 HTML 元素產(chǎn)生的動態(tài)變化效果。
實例
勾選復選框隱藏 DIV:
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> div { transition: all linear 0.5s; background-color: lightblue; height: 100px; width: 100%; position: relative; top: 0; left: 0; } .ng-hide { height: 0; width: 0; background-color: transparent; top:-200px; left: 200px; } </style> <script src="//cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script> <script src="//cdn.bootcss.com/angular.js/1.4.6/angular-animate.min.js"></script> </head> <body ng-app="ngAnimate"> <h1>隱藏 DIV: <input type="checkbox" ng-model="myCheck"></h1> <div ng-hide="myCheck"></div> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
![]() | 應用中動畫不宜太多,但合適的使用動畫可以增加頁面的豐富性,也可以更易讓用戶理解。 |
---|
如果我們應用已經(jīng)設置了應用名,可以把 ngAnimate 直接添加在模型中:
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> div { transition: all linear 0.5s; background-color: lightblue; height: 100px; width: 100%; position: relative; top: 0; left: 0; } .ng-hide { height: 0; width: 0; background-color: transparent; top:-200px; left: 200px; } </style> <script src="//cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script> <script src="//cdn.bootcss.com/angular.js/1.4.6/angular-animate.min.js"></script> </head> <body ng-app="myApp"> <h1>隱藏 DIV: <input type="checkbox" ng-model="myCheck"></h1> <div ng-hide="myCheck"></div> <script> var app = angular.module('myApp', ['ngAnimate']); </script> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
ngAnimate 做了什么?
ngAnimate 模型可以添加或移除 class 。
ngAnimate 模型并不能使 HTML 元素產(chǎn)生動畫,但是 ngAnimate 會監(jiān)測事件,類似隱藏顯示 HTML 元素 ,如果事件發(fā)生 ngAnimate 就會使用預定義的 class 來設置 HTML 元素的動畫。
AngularJS 添加/移除 class 的指令:
ng-show
ng-hide
ng-class
ng-view
ng-include
ng-repeat
ng-if
ng-switch
ng-show
和 ng-hide
指令用于添加或移除 ng-hide
class 的值。
其他指令會在進入 DOM 會添加 ng-enter
類,移除 DOM 會添加 ng-leave
屬性。
當 HTML 元素位置改變時,ng-repeat
指令同樣可以添加 ng-move
類 。
此外, 在動畫完成后,HTML 元素的類集合將被移除。例如:ng-hide
指令會添加一下類:
ng-animate
ng-hide-animate
ng-hide-add
(如果元素將被隱藏)ng-hide-remove
(如果元素將顯示)ng-hide-add-active
(如果元素將隱藏)ng-hide-remove-active
(如果元素將顯示)
使用 CSS 動畫
我們可以使用 CSS transition(過渡) 或 CSS 動畫讓 HTML 元素產(chǎn)生動畫效果,該部分內(nèi)容你可以參閱我們的CSS 過渡教程,CSS 動畫教程。
CSS 過渡
CSS 過渡可以讓我們平滑的將一個 CSS 屬性值修改為另外一個:
實例
在 DIV 元素設置了 .ng-hide
類時,過渡需要花費 0.5 秒,高度從 100px 變?yōu)?0:
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> div { transition: all linear 0.5s; background-color: lightblue; height: 100px; } .ng-hide { height: 0; } </style> <script src="//cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script> <script src="//cdn.bootcss.com/angular.js/1.4.6/angular-animate.min.js"></script> </head> <body ng-app="myApp"> <h1>隱藏 DIV: <input type="checkbox" ng-model="myCheck"></h1> <div ng-hide="myCheck"></div> <script> var app = angular.module('myApp', ['ngAnimate']); </script> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例
CSS 動畫
CSS 動畫允許你平滑的修改 CSS 屬性值:
實例
在 DIV 元素設置了 .ng-hide
類時, myChange
動畫將執(zhí)行,它會平滑的將高度從 100px 變?yōu)?0:
實例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> @keyframes myChange { from { height: 100px; } to { height: 0; } } div { height: 100px; background-color: lightblue; } div.ng-hide { animation: 0.5s myChange; } </style> <script src="//cdn.bootcss.com/angular.js/1.4.6/angular.min.js"></script> <script src="//cdn.bootcss.com/angular.js/1.4.6/angular-animate.min.js"></script> </head> <body ng-app="ngAnimate"> 隱藏 DIV: <input type="checkbox" ng-model="myCheck"> <div ng-hide="myCheck"> </div> </body> </html>
運行實例 ?
點擊 "運行實例" 按鈕查看在線實例