AngularJS 動(dòng)畫
AngularJS 提供了動(dòng)畫效果,可以配合 CSS 使用。
AngularJS 使用動(dòng)畫需要引入 angular-animate.min.js 庫(kù)。
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular-animate.min.js"></script>
還需在應(yīng)用中使用模型 ngAnimate:
<body ng-app="ngAnimate">
什么是動(dòng)畫?
動(dòng)畫是通過改變 HTML 元素產(chǎn)生的動(dòng)態(tài)變化效果。
實(shí)例
勾選復(fù)選框隱藏 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="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script><script src="http://apps.bdimg.com/libs/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>
運(yùn)行效果:
注意:應(yīng)用中動(dòng)畫不宜太多,但合適的使用動(dòng)畫可以增加頁(yè)面的豐富性,也可以更易讓用戶理解。
如果我們應(yīng)用已經(jīng)設(shè)置了應(yīng)用名,可以把 ngAnimate 直接添加在模型中:
實(shí)例
<!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="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script><script src="http://apps.bdimg.com/libs/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>
運(yùn)行效果:
ngAnimate 做了什么?
ngAnimate 模型可以添加或移除 class 。
ngAnimate 模型并不能使 HTML 元素產(chǎn)生動(dòng)畫,但是 ngAnimate 會(huì)監(jiān)測(cè)事件,類似隱藏顯示 HTML 元素 ,如果事件發(fā)生 ngAnimate 就會(huì)使用預(yù)定義的 class 來設(shè)置 HTML 元素的動(dòng)畫。
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 的值。
其他指令會(huì)在進(jìn)入 DOM 會(huì)添加 ng-enter 類,移除 DOM 會(huì)添加 ng-leave 屬性。
當(dāng) HTML 元素位置改變時(shí),ng-repeat 指令同樣可以添加 ng-move 類 。
此外, 在動(dòng)畫完成后,HTML 元素的類集合將被移除。例如: ng-hide 指令會(huì)添加一下類:
ng-animate
ng-hide-animate
ng-hide-add (如果元素將被隱藏)
ng-hide-remove (如果元素將顯示)
ng-hide-add-active (如果元素將隱藏)
ng-hide-remove-active (如果元素將顯示)
使用 CSS 動(dòng)畫
我們可以使用 CSS transition(過渡) 或 CSS 動(dòng)畫讓 HTML 元素產(chǎn)生動(dòng)畫效果,該部分內(nèi)容你可以參閱我們的 CSS 過渡教程, CSS 動(dòng)畫教程。
CSS 過渡
CSS 過渡可以讓我們平滑的將一個(gè) CSS 屬性值修改為另外一個(gè):
實(shí)例
在 DIV 元素設(shè)置了 .ng-hide 類時(shí),過渡需要花費(fèi) 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="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script><script src="http://apps.bdimg.com/libs/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 動(dòng)畫
CSS 動(dòng)畫允許你平滑的修改 CSS 屬性值:
實(shí)例
在 DIV 元素設(shè)置了 .ng-hide 類時(shí), myChange 動(dòng)畫將執(zhí)行,它會(huì)平滑的將高度從 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="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script><script src="http://apps.bdimg.com/libs/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>
以上就是對(duì)AngularJS 動(dòng)畫的資料整理,有需要的小伙伴參考下。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注