利用CSS3在Angular中實(shí)現(xiàn)動(dòng)畫
廢話不多說(shuō)了,直接給大家貼實(shí)例代碼。
直接看例子:
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ngAnimate插件1</title> <script type="text/javascript" src="https://code.angularjs.org/1.3.8/angular.min.js"></script>
<script type="text/javascript" src="https://code.angularjs.org/1.3.8/angular-animate.min.js"></script> <style type="text/css">
.box{width:200px;height:200px;background-color:red;transition:1s all;}
/*顯示操作*/
.box.ng-enter{opacity:0;}
.box.ng-enter-active{opacity:1;}
/*隱藏操作*/
.box.ng-leave{opacity:1;}
.box.ng-leave-active{opacity:0;}
</style>
</head>
<body>
<div ng-controller="Aaa">
<input type="checkbox" ng-model="bBtn">
<div class="box" ng-if="bBtn"></div>
</div>
<script type="text/javascript">
var m1 = angular.module('myApp',['ngAnimate']);
m1.controller('Aaa',['$scope',function($scope){
$scope.bBtn = true;
}]);
</script>
</body>
</html>
引入angular-animate插件,我們綁定了ng-if指令,在刪除和添加DOM節(jié)點(diǎn)的時(shí)候,angular會(huì)添加指定的class,方便我們完成動(dòng)畫。
.ng-enter
.ng-enter-active
.ng-leave
.ng-leave-active
現(xiàn)在再看看顯示和隱藏。
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ngAnimate插件4</title>
<script type="text/javascript" src="https://code.angularjs.org/1.3.8/angular.min.js"></script>
<script type="text/javascript" src="https://code.angularjs.org/1.3.8/angular-animate.min.js"></script>
<style type="text/css">
.box{width:200px;height:200px;background-color:red;transition:1s all;}
/*顯示操作*/
.box.ng-hide-remove{opacity:0;}
.box.ng-hide-remove-active{opacity:1;}
/*隱藏操作*/
.box.ng-hide-add{opacity:1;}
.box.ng-hide-add-active{opacity:0;}
</style>
</head>
<body>
<div ng-controller="Aaa">
<input type="checkbox" ng-model="bBtn">
<div class="box" ng-show="bBtn"></div>
</div>
<script type="text/javascript">
var m1 = angular.module('myApp',['ngAnimate']);
m1.controller('Aaa',['$scope',function($scope){
$scope.bBtn = true;
}]);
</script>
</body>
</html>
.ng-hide-remove
.ng-hide-remove-active
.ng-hide-add
.ng-hide-add-active
這個(gè)例子我們使用的是ng-show,屬于顯示和隱藏。上一個(gè)例子是ng-if,屬于添加和刪除。
回顧上一節(jié)我們提到的路由,我們可以結(jié)合起來(lái)操作。
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ngAnimate插件2</title> <script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript" src="http://cdn.bootcss.com/angular.js/1.3.0-beta.13/angular-route.min.js"></script>
<script type="text/javascript" src="http://cdn.bootcss.com/angular.js/1.3.0-beta.13/angular-animate.min.js"></script> <style type="text/css">
.box{transition:1s all;position:absolute;}
/*顯示操作*/
.box.ng-enter{opacity:0;}
.box.ng-enter-active{opacity:1;}
/*隱藏操作*/
.box.ng-leave{opacity:1;}
.box.ng-leave-active{opacity:0;}
</style>
</head>
<body>
<div ng-controller="Aaa">
<a href="javascript:void(0);" ng-click="$location.path('aaa')">首頁(yè)</a>
<a href="javascript:void(0);" ng-click="$location.path('bbb')">內(nèi)容</a>
<a href="javascript:void(0);" ng-click="$location.path('ccc')">標(biāo)題</a>
<div class="box" ng-view></div>
</div>
<script type="text/javascript">
var m1 = angular.module('myApp',['ngRoute','ngAnimate']);
m1.config(['$routeProvider',function($routeProvider){
$routeProvider.when('/aaa',{
template : '<h1>AAA</h1>{{name}}',
controller : 'Aaa'
}).when('/bbb',{
template : '<h1>BBB</h1>{{name}}',
controller : 'Bbb'
}).when('/ccc',{
template : '<h1>CCC</h1>{{name}}',
controller : 'Ccc'
}).otherwise({
redirectTo : '/aaa'
});
}]);
m1.controller('Aaa',['$scope','$location','$routeParams',function($scope,$location,$routeParams){
$scope.name = 'xiecg-Aaa';
$scope.$location = $location;
}]);
m1.controller('Bbb',['$scope',function($scope){
$scope.name = 'xiecg-Bbb';
}]);
m1.controller('Ccc',['$scope',function($scope){
$scope.name = 'xiecg-Ccc';
}]);
</script>
</body>
</html>
這樣在切換頁(yè)面的時(shí)候就有淡入淡出的效果。
再回顧前面的幾章講的百度搜索:
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ngAnimate插件3</title> <script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript" src="https://code.angularjs.org/1.3.8/angular.min.js"></script>
<script type="text/javascript" src="https://code.angularjs.org/1.3.8/angular-animate.min.js"></script> <style type="text/css">
.box{transition:1s all;}
/*顯示操作*/
.box.ng-enter{opacity:0;}
.box.ng-enter-active{opacity:1;}
/*隱藏操作*/
.box.ng-leave{display:none;}
.box.ng-enter-stagger{animation-delay:0.1s;}
</style>
</head>
<body>
<div ng-controller="Aaa">
<input type="text" ng-model="name" ng-keyup="change(name)">
<input type="button" ng-click="change(name)" value="搜索">
<ul>
<li class="box" ng-repeat="d in data">{wppm3vysvbp}</li>
</ul>
</div>
<script type="text/javascript">
var m1 = angular.module('myApp',['ngAnimate']);
m1.controller('Aaa',['$scope','$http','$timeout',function($scope,$http,$timeout){
var timer = null;
$scope.data = [];
$scope.change = function(name){
$timeout.cancel(timer);
timer = $timeout(function(){
$http({
method : 'JSONP',
url : 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd='+name+'&cb=JSON_CALLBACK',
}).success(function(data,state,headers,config){
console.log(data);
$scope.data = data.s;
}).error(function(data){
console.log(data);
});
},500);
};
}]);
</script>
</body>
</html>
通過(guò)跨域我們得到百度返回過(guò)來(lái)的數(shù)據(jù),依次過(guò)渡顯示到頁(yè)面上。
下面來(lái)看JS動(dòng)畫的例子:
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ngAnimate插件5</title>
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript" src="https://code.angularjs.org/1.3.8/angular.min.js"></script>
<script type="text/javascript" src="https://code.angularjs.org/1.3.8/angular-animate.min.js"></script>
<style type="text/css">
.box{width:200px;height:200px;background-color:red;}
</style>
</head>
<body>
<div ng-controller="Aaa">
<input type="checkbox" ng-model="bBtn">
<div class="box" ng-if="bBtn"></div>
</div>
<script type="text/javascript">
var m1 = angular.module('myApp',['ngAnimate']);
//ng-if
m1.animation('.box',function(){
return {
//hide(刪除)
leave : function(element,done){
//console.log(element,done); //元素節(jié)點(diǎn)&刪除節(jié)點(diǎn)的回調(diào)函數(shù)
$(element).animate({
width : 0,
height : 0
},1000,done);
},
//show(填充)
enter : function(element,done){
//ng-if會(huì)動(dòng)態(tài)創(chuàng)建元素,元素默認(rèn)就有200的高寬。。。
$(element).css({
width : 0,
height : 0
}).animate({
width : 200,
height : 200
},1000,done);
}
};
});
m1.controller('Aaa',['$scope',function($scope){
$scope.bBtn = true;
}]);
</script>
</body>
</html>
JS動(dòng)畫我們使用JQ的動(dòng)畫庫(kù)來(lái)完成,注意我們?cè)谝晥D上使用的是ng-if,表示添加和刪除DOM節(jié)點(diǎn),所以我們?cè)诳刂破鱮eturn leave&enter。
JS動(dòng)畫有了ng-if,自然就是ng-show。
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ngAnimate插件5</title>
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript" src="https://code.angularjs.org/1.3.8/angular.min.js"></script>
<script type="text/javascript" src="https://code.angularjs.org/1.3.8/angular-animate.min.js"></script>
<style type="text/css">
.box{width:200px;height:200px;background-color:red;}
</style>
</head>
<body>
<div ng-controller="Aaa">
<input type="checkbox" ng-model="bBtn">
<div class="box" ng-show="bBtn"></div>
</div>
<script type="text/javascript">
var m1 = angular.module('myApp',['ngAnimate']);
//ng-show
m1.animation('.box',function(){
return {
//hide(隱藏)
addClass : function(element,sClass,done){
//console.log(element,sClass,done); //元素節(jié)點(diǎn)&樣式名&刪除節(jié)點(diǎn)的回調(diào)函數(shù)
$(element).animate({
width : 0,
height : 0
},1000,done);
},
//show(顯示)
removeClass : function(element,sClass,done){
$(element).animate({
width : 200,
height : 200
},1000,done);
}
};
});
m1.controller('Aaa',['$scope',function($scope){
$scope.bBtn = true;
}]);
</script>
</body>
</html>
在控制器return addClass&removeClass,表示隱藏和顯示。
- Angular4.0動(dòng)畫操作實(shí)例詳解
- Angular4如何自定義首屏的加載動(dòng)畫詳解
- Angular2搜索和重置按鈕過(guò)場(chǎng)動(dòng)畫
- 基于Angular.js實(shí)現(xiàn)的觸摸滑動(dòng)動(dòng)畫實(shí)例代碼
- 給angular加上動(dòng)畫效遇到的問(wèn)題總結(jié)
- AngularJS中實(shí)現(xiàn)顯示或隱藏動(dòng)畫效果的方式總結(jié)
- 使用ngView配合AngularJS應(yīng)用實(shí)現(xiàn)動(dòng)畫效果的方法
- 在AngularJS應(yīng)用中實(shí)現(xiàn)一些動(dòng)畫效果的代碼
- 詳解Angular路由動(dòng)畫及高階動(dòng)畫函數(shù)
相關(guān)文章
AngularJs返回前一頁(yè)面時(shí)刷新一次前面頁(yè)面的方法
今天小編就為大家分享一篇AngularJs返回前一頁(yè)面時(shí)刷新一次前面頁(yè)面的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
AngularJS中update兩次出現(xiàn)$promise屬性無(wú)法識(shí)別的解決方法
最近在工作中用AngularJS中update了兩次之后發(fā)現(xiàn)$promise屬性無(wú)法識(shí)別了,后來(lái)通過(guò)查找相關(guān)的資料終于解決了,想著記錄下方便自己或者有需要的朋友,所以本文主要介紹了AngularJS中update兩次出現(xiàn)了$promise屬性無(wú)法識(shí)別的解決方法,需要的朋友可以參考借鑒。2017-01-01
AngularJS服務(wù)service用法總結(jié)
這篇文章主要介紹了AngularJS服務(wù)service用法,結(jié)合實(shí)例形式總結(jié)分析了服務(wù)Service的概念、功能及自定義服務(wù)的相關(guān)操作技巧,需要的朋友可以參考下2016-12-12
angularJs select綁定的model取不到值的解決方法
今天小編就為大家分享一篇angularJs select綁定的model取不到值的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
使用AngularJS對(duì)路由進(jìn)行安全性處理的方法
這篇文章主要介紹了使用AngularJS對(duì)路由進(jìn)行安全性處理的方法,AngularJS是一款高人氣的JavaScript庫(kù),需要的朋友可以參考下2015-06-06
詳解AngularJs中$resource和restfu服務(wù)端數(shù)據(jù)交互
之前小編和大家分享過(guò)使用$http同服務(wù)器進(jìn)行通信,但是功能上比較簡(jiǎn)單,angularjs還提供了另外一個(gè)可選的服務(wù)$resource,使用它可以非常方便的同支持restful的服務(wù)單進(jìn)行數(shù)據(jù)交互。下面來(lái)一起看看吧。2016-09-09

