当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > AngularJS中实现动画效果的方法

AngularJS中实现动画效果的方法

2018年05月29日  | 移动技术网IT编程  | 我要评论

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 元素产生的动态变化效果。

实例

勾选复选框隐藏 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>

运行效果:

注意:应用中动画不宜太多,但合适的使用动画可以增加页面的丰富性,也可以更易让用户理解。

如果我们应用已经设置了应用名,可以把 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="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>

运行效果:

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

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 元素产生动画效果,该部分内容你可以参阅我们的 css 过渡教程, css 动画教程。

css 过渡

css 过渡可以让我们平滑的将一个 css 属性值修改为另外一个:

实例

在 div 元素设置了 .ng-hide 类时,过渡需要花费 0.5 秒,高度从 100px 变为 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 动画

css 动画允许你平滑的修改 css 属性值:

实例

在 div 元素设置了 .ng-hide 类时, mychange 动画将执行,它会平滑的将高度从 100px 变为 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>

以上就是对angularjs 动画的资料整理,有需要的小伙伴参考下。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网