国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

AngularJS中實(shí)現(xiàn)動(dòng)畫效果的方法

2019-11-20 09:23:06
字體:
供稿:網(wǎng)友

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)畫的資料整理,有需要的小伙伴參考下。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 丰镇市| 历史| 辉南县| 清水河县| 华容县| 密山市| 唐山市| 五寨县| 南丹县| 四平市| 和平县| 霍林郭勒市| 庄浪县| 定南县| 花垣县| 高清| 南涧| 汉沽区| 温泉县| 峨山| 平舆县| 勃利县| 台中县| 三台县| 安义县| 酉阳| 益阳市| 白沙| 调兵山市| 陇川县| 沙湾县| 门头沟区| 蒙阴县| 罗江县| 罗山县| 五寨县| 安阳市| 额敏县| 凤阳县| 叶城县| 云阳县|