如何使用angular-ui-bootstrap将淡入淡出动画添加到标签集?
例如,给出以下代码:
<tabset>
<tab heading="Tab1">Some content</tab>
<tab heading="Tab2">Other content</tab>
</tabset>
我希望标签的内容在它们之间切换时淡出。我尝试将fade
类添加到tab
标记(类似于使用bootstrap3 js文件的方式),但它不起作用。
非常感谢!
答案 0 :(得分:27)
由于tabset使用ng-class
来控制&#34; active&#34;选项卡,允许我们通过在&#34;活动&#34;时设置不透明度= 0来定义具有角度动画的淡入淡出效果课程被删除/附加。
首先,您需要加载ngAnimate
模块,包括angular-animate.js
并设置依赖关系。
添加到您的<head>
:
<script src="https://code.angularjs.org/1.2.24/angular-animate.js"></script>
设置模块依赖:
angular.module("myApp", ["ui.bootstrap", "ngAnimate"]);
现在将动画类添加到您的tabset。
<tabset class="tab-animation">
<tab heading="Tab1">Some content</tab>
<tab heading="Tab2">Other content</tab>
</tabset>
将以下代码放入css文件中:
/* set reference point */
.tab-animation > .tab-content {
position: relative;
}
/* set animate effect */
.tab-animation > .tab-content > .tab-pane{
transition: 0.2s linear opacity;
}
/* overwrite display: none and remove from document flow */
.tab-animation > .tab-content > .tab-pane.active-remove {
position: absolute;
top: 0;
width: 100%;
display: block;
}
/* opacity=0 when removing "active" class */
.tab-animation > .tab-content > .tab-pane.active-remove-active {
opacity: 0;
}
/* opacity=0 when adding "active" class */
.tab-animation > .tab-content > .tab-pane.active-add {
opacity: 0;
}
这就是全部。您可以查看demo on Plunker。
另请查看ngAnimate doc。
答案 1 :(得分:2)
我最终修补了ui-bootstrap文件。
我仍然是AngularJS的菜鸟,所以请原谅语言。
这是一个非传统的黑客攻击,需要用ng-animate重构,但它有效。
打开 ui-bootstrap-tpls-0.10.0.js 并查找“tab”指令:
.directive('tab', ['$parse', function($parse) {
return {
require: '^tabset',
restrict: 'EA',
replace: true,
templateUrl: 'template/tabs/tab.html',
transclude: true,
scope: {
id:'@', // PATCH : GETTING TAB 'id' ATTRIBUTE
heading: '@',
onSelect: '&select', //This callback is called in contentHeadingTransclude
//once it inserts the tab's content into the dom
onDeselect: '&deselect'
},
// ...
注意检索id属性值的额外代码(通过翻译,我猜)。
下面几行,寻找:
scope.$watch('active', function(active) {
并按照这样修补:
scope.$watch('active', function(active) {
// Note this watcher also initializes and assigns scope.active to the
// attrs.active expression.
setActive(scope.$parent, active);
if (active) {
tabsetCtrl.select(scope);
scope.onSelect();
tab_id = attrs.id;
$(".tab_pane_"+tab_id).hide(); // HIDE AT FIRST, SO IT CAN ACTUALLY FADE IN
$(".tab_pane_"+tab_id).fadeIn(1000); // JQUERY TARGETING BY CLASS
} else {
scope.onDeselect();
tab_id = attrs.id;
$(".tab_pane_"+tab_id).hide(); // JQUERY TARGETING BY CLASS
}
});
下面几行,寻找:
scope.select = function() {
并添加内部:
$(".tab-pane").hide();
所以所有标签窗格首先都隐藏正确。
然后,寻找:
angular.module("template/tabs/tabset.html", []).run(["$templateCache", function($templateCache) { ...
并将css类添加到相应模板中的tab-pane元素,如下所示:
angular.module("template/tabs/tabset.html", []).run(["$templateCache", function($templateCache) {
$templateCache.put("template/tabs/tabset.html",
"\n" +
"<div class=\"tabbable\">\n" +
" <ul class=\"nav {{type && 'nav-' + type}}\" ng-class=\"{'nav-stacked': vertical, 'nav-justified': justified}\" ng-transclude></ul>\n" +
" <div class=\"tab-content\">\n" +
" <div class=\"tab-pane tab_pane_{{tab.id}}\" \n" + // CLASS NAME IS DYNAMIC
" ng-repeat=\"tab in tabs\" \n" +
" ng-class=\"{active: tab.active}\"\n" +
" tab-content-transclude=\"tab\">\n" +
" </div>\n" +
" </div>\n" +
"</div>\n" +
"");
}]);
修改ui-bootstrap .js文件后,您必须编辑视图模板(获取选项卡的位置)并声明'id'属性:
<!-- TABS -->
<tabset justified="true">
<tab ng-repeat="tab in tabs" heading="{{tab.title}}" id="{{tab.id}}" >
// ... TAB CONTENT
你应该得到基本的概念,目前它不是很优雅(温和地说)。
但它确实有效。
如果你想知道我的标签是如何获得id的,那么,我将它们注入我的控制器中:
Tab1 = {
id:1,
'ShortDescription': ShortDescription,
'FullDescription': FullDescription,
'TabContent': TabContent1,
title: "ProductTabTitleDefault1",
// active:true
};
Tab2 = {
id:2,
'ShortDescription': ShortDescription,
'FullDescription': FullDescription,
'TabContent': TabContent1,
title: "ProductTabTitleDefault2",
// active:true
};
$rootScope.tabs = {
'Tab1': Tab1,
'Tab2': Tab2,
};
当然这是模型数据,但假设您的标签及其内容是动态的,您可以使用计数器,也可以使用其他密钥而不是“id”(但您必须相应地更改其余部分)。 / p>
答案 2 :(得分:1)
我有一个替代解决方案@ user3413125上面写得很好的解决方案。它使用@keyframes来实现交叉渐变,而不是淡出然后淡入。请参阅demo on Plunker
这是CSS的淡入部分(淡出类似):
.tab-animation > .tab-content > .tab-pane.active-add {
animation: 1s fade-in;
}
@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}
关键帧技术取自AngularJs tutorial 14 - 寻找“CSS关键帧动画:动画ngView”,大约在页面的一半。