AngularJS中實(shí)現(xiàn)顯示或隱藏動(dòng)畫(huà)效果的方式總結(jié)
AngularJS 是一組用于創(chuàng)建單頁(yè)Web應(yīng)用的豐富框架,給構(gòu)建豐富交互地應(yīng)用帶來(lái)了所有需要的功能。其中一項(xiàng)主要的特性就是Angular帶來(lái)了對(duì)動(dòng)畫(huà)的支持。
本篇體驗(yàn)在AngularJS中實(shí)現(xiàn)在"顯示/隱藏"這2種狀態(tài)切換間添加動(dòng)畫(huà)效果。
通過(guò)CSS方式實(shí)現(xiàn)顯示/隱藏動(dòng)畫(huà)效果
思路:
→npm install angular-animage
→依賴(lài):var app = angular.module("app",["ngAnimate"]);
→controller中一個(gè)變量接收bool值
→界面中提供一個(gè)按鈕,點(diǎn)擊改變bool值
→界面中顯示/隱藏的區(qū)域提供ng-if和controller中的bool值綁定
app.js
var app = angular.module("app",["ngAnimate"]); app.controller("AppCtrl", function(){ this.toggle = true; })
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.min.css"/> <link rel="stylesheet" href="styles.css"/> </head> <body ng-app="app" ng-controller="AppCtrl as app"> <button class="btn" ng-click="app.toggle=!app.toggle">Toggle Animation</button> <div class="toggle" ng-if="app.toggle">Some content here</div> <script src="../node_modules/angular/angular.min.js"></script> <script src="../node_modules/angular-animate/angular-animate.min.js"></script> <script src="app.js"></script> </body> </html>
styes.css
.toggle{ -webkit-transition: linear 1s; -moz-transition: linear 1s; -ms-transition: linear 1s; -o-transition: linear 1s; transition: linear 1s; } .toggle.ng-enter{ opacity: 0; } .toggle.ng-enter-active{ opacity: 1; } .toggle.ng-leave{ opacity: 1; } .toggle.ng-leave-active{ opacity: 0; }
通過(guò)animation方法實(shí)現(xiàn)顯示/隱藏動(dòng)畫(huà)效果
app.animation("某個(gè)類(lèi)名", function(){ return { leave: function(element, done){ }, enter: function(element, done){ } } })
animation可以在某個(gè)類(lèi)名上加上leave,enter事件,leave和enter函數(shù)內(nèi)部如何實(shí)現(xiàn)動(dòng)畫(huà)效果呢?可以通過(guò)TweenMax.min.js實(shí)現(xiàn)。
app1.js
ar app = angular.module("app",["ngAnimate"]); app.controller("AppCtrl", function(){ this.toggle = true; }) app.animation(".toggle", function(){ return { leave: function(element, done){ //element.text("nooooo"); TweenMax.to(element, 1, {opacity:0, onComplete:done}) }, enter: function(element, done){ TweenMax.from(element, 1, {opacity:0, onComplete:done}) } } })
index1.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="../node_modules/topcoat/css/topcoat-desktop-light.min.css"/> </head> <body class="well-lg" ng-app="app" ng-controller="AppCtrl as app"> <button class="topcoat-button--large--cta" ng-click="app.toggle = !app.toggle">點(diǎn)我</button> <hr/> <div class="topcoat-notification toggle" ng-if="app.toggle">I'm too your to fade</div> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.0/TweenMax.min.js"></script> <script src="../node_modules/angular/angular.min.js"></script> <script src="../node_modules/angular-animate/angular-animate.min.js"></script> <script src="app1.js"></script> </body> </html>
其中,npm install topcoat是一個(gè)很好的樣式庫(kù)。
使用direcive的方式實(shí)現(xiàn)顯示/隱藏動(dòng)畫(huà)效果
是否可以在顯示/隱藏的div部分加上一個(gè)屬性,比如hide-me="app.isHidden",hide-me這個(gè)屬性監(jiān)控app.isHidden,根據(jù)值的變化情況再來(lái)決定是否顯示。
app3.js
var app=angular.module('app',["ngAnimate"]); app.controller("AppCtrl", function(){ this.isHidden = false; this.fadeIt = function(){ //TweenMax.to($("#my-badge"), 1, {opacity:0}) this.isHidden = !this.isHidden; } }) app.directive("hideMe", function($animate){ return function(scope, element, attrs){ scope.$watch(attrs.hideMe, function(newVal){ if(newVal){ //TweenMax.to(element, 1, {opacity:0}); $animate.addClass(element, "fade"); } else{ $animate.removeClass(element, "fade"); } }) } }) app.animation(".fade", function(){ return { addClass: function(element, className){ TweenMax.to(element, 1, {opacity:0}); }, removeClass: function(element, className){ TweenMax.to(element, 1, {opacity:1}); } } })
index3.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.min.css"/> </head> <body ng-app="app" ng-controller="AppCtrl as app"> <button id="my-button" class="btn-primary" ng-click="app.fadeIt()">Click to fade</button> <div id="my-badge" class="badge" hide-me="app.isHidden">Fade me</div> <script src="../node_modules/jquery/dist/jquery.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.18.0/TweenMax.min.js"></script> <script src="../node_modules/angular/angular.min.js"></script> <script src="../node_modules/angular-animate/angular-animate.min.js"></script> <script src="app3.js"></script> </body> </html>
以上內(nèi)容是小編給大家介紹的AngularJS中實(shí)現(xiàn)顯示或隱藏動(dòng)畫(huà)效果的方式總結(jié),希望大家喜歡。
- Angular4.0動(dòng)畫(huà)操作實(shí)例詳解
- Angular4如何自定義首屏的加載動(dòng)畫(huà)詳解
- Angular2搜索和重置按鈕過(guò)場(chǎng)動(dòng)畫(huà)
- 基于Angular.js實(shí)現(xiàn)的觸摸滑動(dòng)動(dòng)畫(huà)實(shí)例代碼
- 給angular加上動(dòng)畫(huà)效遇到的問(wèn)題總結(jié)
- 利用CSS3在Angular中實(shí)現(xiàn)動(dòng)畫(huà)
- 使用ngView配合AngularJS應(yīng)用實(shí)現(xiàn)動(dòng)畫(huà)效果的方法
- 在AngularJS應(yīng)用中實(shí)現(xiàn)一些動(dòng)畫(huà)效果的代碼
- 詳解Angular路由動(dòng)畫(huà)及高階動(dòng)畫(huà)函數(shù)
相關(guān)文章
AngularJs bootstrap搭載前臺(tái)框架——準(zhǔn)備工作
本文主要介紹AngularJs bootstrap搭載前臺(tái)框架,這里對(duì)Bootstrap 搭載環(huán)境,及注意事項(xiàng)做了講解,有需要的小伙伴可以參考下2016-09-09Angular 2 利用Router事件和Title實(shí)現(xiàn)動(dòng)態(tài)頁(yè)面標(biāo)題的方法
本篇文章主要介紹了Angular 2 利用Router事件和Title實(shí)現(xiàn)動(dòng)態(tài)頁(yè)面標(biāo)題的方法,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08AngularJs的$http發(fā)送POST請(qǐng)求,php無(wú)法接收Post的數(shù)據(jù)問(wèn)題及解決方案
這篇文章主要介紹了AngularJs的$http發(fā)送POST請(qǐng)求,php無(wú)法接收Post的數(shù)據(jù)的問(wèn)題及解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Angularjs過(guò)濾器實(shí)現(xiàn)動(dòng)態(tài)搜索與排序功能示例
這篇文章主要介紹了Angularjs過(guò)濾器實(shí)現(xiàn)動(dòng)態(tài)搜索與排序功能,涉及AngularJS過(guò)濾器相關(guān)搜索、查詢(xún)、排序操作技巧,需要的朋友可以參考下2017-12-12使用Angular.js開(kāi)發(fā)的注意事項(xiàng)
這篇文章主要記錄了一些在學(xué)習(xí)和使用angular.js踩到的坑和需要注意的點(diǎn),方便以后自己查閱,也給同樣遇到這些問(wèn)題的朋友們一些幫助,有需要的朋友們下面來(lái)一起看看吧。2016-10-10