如何使用带有 AngularJS 的 ng-hide 淡出元素?
How to fade out element using ng-hide with AngularJS?
我目前可以 show/hide 根据控制器中的布尔条件确定元素。如果条件为真,我如何淡出元素而不是立即隐藏它?
<div id='conversion-image' ng-hide="pages.length > 0">
generating pages...
<br />
<img src='Images/ajax-loader-blue.gif' />
</div>
我也已经包含了 ngAnimate 依赖项。
var app = angular.module("app", ["ngAnimate"]);
您可以使用 CSS 和 ng-animate
来完成,如下所示:
.fade-out.ng-hide {
opacity: 0;
}
.fade-out.ng-hide-add, .fade-out.ng-hide-remove {
transition: all linear 0.5s;
}
.check-element {
border: 1px solid black;
opacity: 1;
padding: 10px;
}
<head>
<meta charset="UTF-8">
<link href="animations.css" rel="stylesheet" type="text/css">
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
<script src="//code.angularjs.org/snapshot/angular-animate.js"></script>
</head>
<body ng-app="ngAnimate">
Hide: <input type="checkbox" ng-model="checked" aria-label="Toggle ngHide"><br />
<div class="check-element fade-out" ng-hide="checked">
I fade out when your checkbox is checked.
</div>
</body>
Overriding .ng-hide
By default, the .ng-hide class will style the element with display: none !important. If you wish to change the hide behavior with ngShow/ngHide, you can simply overwrite the styles for the .ng-hide CSS class. Note that the selector that needs to be used is actually .ng-hide:not(.ng-hide-animate) to cope with extra animation classes that can be added.
添加以下内容classclass="animate-show animate-hide"
<div id='conversion-image' class="animate-show animate-hide" ng-
hide="model.transaction.selectedDocument.pages.length > 0">
generating pages...
<br />
<img src='Images/ajax-loader-blue.gif' />
</div>
添加以下样式
<style>
.animate-show,.animate-hide {
-webkit-transition:all linear 1s;
-moz-transition:all linear 1s;
-ms-transition:all linear 1s;
-o-transition:all linear 1s;
transition:all linear 1s;
}
.animate-show.ng-hide-remove,
.animate-hide.ng-hide-add.ng-hide-add-active {
opacity: 0;
display: block !important;
}
.animate-hide.ng-hide-add,
.animate-show.ng-hide-remove.ng-hide-remove-active {
opacity: 1;
display: block !important;
</style>
这是一个 plunker 示例 http://plnkr.co/edit/VoWwmHK57wtuyGl6npr0?p=preview
只需在 div 中使用淡入淡出 class 并添加一个按钮来切换条件以查看隐藏时的动画。你可以在 ng-hide = "your_own_condition"
中使用你自己的条件
<div id='conversion-image' class="fade" ng-hide="condition">
generating pages...
<br />
<img src='Images/ajax-loader-blue.gif' />
</div>
<button ng-click="condition=!condition">Toggle</button>
<style>
.fade.ng-hide {
transition:0.5s linear all;
opacity:0;
}
</style>
我目前可以 show/hide 根据控制器中的布尔条件确定元素。如果条件为真,我如何淡出元素而不是立即隐藏它?
<div id='conversion-image' ng-hide="pages.length > 0">
generating pages...
<br />
<img src='Images/ajax-loader-blue.gif' />
</div>
我也已经包含了 ngAnimate 依赖项。
var app = angular.module("app", ["ngAnimate"]);
您可以使用 CSS 和 ng-animate
来完成,如下所示:
.fade-out.ng-hide {
opacity: 0;
}
.fade-out.ng-hide-add, .fade-out.ng-hide-remove {
transition: all linear 0.5s;
}
.check-element {
border: 1px solid black;
opacity: 1;
padding: 10px;
}
<head>
<meta charset="UTF-8">
<link href="animations.css" rel="stylesheet" type="text/css">
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
<script src="//code.angularjs.org/snapshot/angular-animate.js"></script>
</head>
<body ng-app="ngAnimate">
Hide: <input type="checkbox" ng-model="checked" aria-label="Toggle ngHide"><br />
<div class="check-element fade-out" ng-hide="checked">
I fade out when your checkbox is checked.
</div>
</body>
Overriding .ng-hide
By default, the .ng-hide class will style the element with display: none !important. If you wish to change the hide behavior with ngShow/ngHide, you can simply overwrite the styles for the .ng-hide CSS class. Note that the selector that needs to be used is actually .ng-hide:not(.ng-hide-animate) to cope with extra animation classes that can be added.
添加以下内容classclass="animate-show animate-hide"
<div id='conversion-image' class="animate-show animate-hide" ng-
hide="model.transaction.selectedDocument.pages.length > 0">
generating pages...
<br />
<img src='Images/ajax-loader-blue.gif' />
</div>
添加以下样式
<style>
.animate-show,.animate-hide {
-webkit-transition:all linear 1s;
-moz-transition:all linear 1s;
-ms-transition:all linear 1s;
-o-transition:all linear 1s;
transition:all linear 1s;
}
.animate-show.ng-hide-remove,
.animate-hide.ng-hide-add.ng-hide-add-active {
opacity: 0;
display: block !important;
}
.animate-hide.ng-hide-add,
.animate-show.ng-hide-remove.ng-hide-remove-active {
opacity: 1;
display: block !important;
</style>
这是一个 plunker 示例 http://plnkr.co/edit/VoWwmHK57wtuyGl6npr0?p=preview
只需在 div 中使用淡入淡出 class 并添加一个按钮来切换条件以查看隐藏时的动画。你可以在 ng-hide = "your_own_condition"
中使用你自己的条件<div id='conversion-image' class="fade" ng-hide="condition">
generating pages...
<br />
<img src='Images/ajax-loader-blue.gif' />
</div>
<button ng-click="condition=!condition">Toggle</button>
<style>
.fade.ng-hide {
transition:0.5s linear all;
opacity:0;
}
</style>