AngularJS入门之动画

AngularJS 提供了动画效果,可以配合 CSS 使用。AngularJS 使用动画需要引入 angular-animate.min.js 库。

<script src="https://apps.bdimg.com/libs/angular.js/1.4.6/angular-animate.min.js"></script>

还需在应用中使用模型 ngAnimate:

<body ng-app="ngAnimate">

1、什么是动画?

动画是通过改变 HTML 元素产生的动态变化效果。

<!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="https://www.jb51.net/js/angular.min.js"></script> <script src="https://www.jb51.net/js/angular-animate.min.js"></script> </head> <body ng-app="ngAnimate"> <h3>隐藏 DIV: <input type="checkbox" ng-model="myCheck"></h3> <div ng-hide="myCheck"></div> </body> </html>

AngularJS入门之动画

如果我们应用已经设置了应用名,可以把 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="https://www.jb51.net/js/angular.min.js"></script> <script src="https://www.jb51.net/js/angular-animate.min.js"></script> </head> <body ng-app="myApp"> <h3>隐藏 DIV: <input type="checkbox" ng-model="myCheck"></h3> <div ng-hide="myCheck"></div> <script> var app = angular.module('myApp', ['ngAnimate']); </script> </body> </html>

2、ngAnimate 做了什么?

ngAnimate 模型可以添加或移除 class 。ngAnimate 模型并不能使 HTML 元素产生动画,但是 ngAnimate 会监测事件,类似隐藏显示 HTML 元素 ,如果事件发生 ngAnimate 就会使用预定义的 class 来设置 HTML 元素的动画。AngularJS 添加/移除 class 的指令:ng-show、ng-hide、ng-class、ng-view、ng-include、ng-repeat、ng-if、ng-switch。

(1)、ng-class指定 HTML 元素使用的 CSS 类

ng-class 指令用于给 HTML 元素动态绑定一个或多个 CSS 类。ng-class 指令的值可以是字符串,对象,或一个数组。如果是字符串,多个类名使用空格分隔。如果是对象,需要使用 key-value 对,key 是一个布尔值,value 为你想要添加的类名。只有在 key 为 true 时类才会被添加。如果是数组,可以由字符串或对象组合组成,数组的元素可以是字符串或对象。

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>angularJs</title> <script src="https://www.jb51.net/js/angular.min.js"></script> <script src="https://www.jb51.net/js/angular-animate.js"></script> <style> .sky { color:white; background-color:lightblue; padding:20px; font-family:"Courier New"; } .tomato { background-color:coral; padding:40px; font-family:Verdana; } </style> </head> <body ng-app=""> <span>选择一个类:</span> <select ng-model="home"> <option value="sky">天空色</option> <option value="tomato">番茄色</option> </select> <div ng-class="home"> <h3>Welcome Home!</h3> <h4>I like it!</h4> </div> </body> </html>

AngularJS入门之动画

(2)、ng-class-even类似 ng-class,但只在偶数行起作用;ng-class-odd 类似 ng-class,但只在奇数行起作用

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>angularJs</title> <script src="https://www.jb51.net/js/angular.min.js"></script> <style> .stripedeven { color:white; background-color:cyan; } .stripedodd{ color:white; background-color:yellowgreen; } </style> </head> <body ng-app="myApp"> <table ng-controller="myCtrl"> <tr> <th>Name</th> <th>Country</th> </tr> <tr ng-repeat="x in records" ng-class-even="'stripedeven'" ng-class-odd="'stripedodd'"> <td>{{x.Name}}</td> <td>{{x.Country}}</td> </tr> </table> <script> var app = angular.module("myApp", []); app.controller("myCtrl", function($scope) { $scope.records = [ { "Name" : "Alfreds Futterkiste", "Country" : "Germany" }, { "Name" : "Berglunds snabbk", "Country" : "Sweden" }, { "Name" : "Centro comercial Moctezuma", "Country" : "Mexico" }, { "Name" : "Ernst Handel", "Country" : "Austria" } ] }); </script> </body> </html>

AngularJS入门之动画

(3)、ng-if如果条件为 false 移除 HTML 元素

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzxspf.html