我正在AngularJS中实现嵌套菜单的幻灯片切换。我一直在使用以下页面中的代码:https://blog.assaf.co/native-slide-toggle-for-angularjs-1-4-x/
虽然我只有一个问题。我试图在带有嵌套子菜单的sidenav菜单上实现此菜单,该子菜单由<ul>
和<li>
元素构建。在切换了具有子菜单的菜单项之后,然后我又切换了该菜单按钮的子菜单,该菜单按钮的<ul>
元素不会滑入。高度保持不变。
此外,如果我保持关闭子菜单,并仅打开顶部菜单项,则在切换子菜单时<ul>
元素的高度保持不变,这意味着直到我切换子菜单才会显示它会打开(尽管仍然不可见),然后关闭顶部菜单按钮,然后再次将其打开。
请在此处查看其运行情况: https://codepen.io/marcus-edensky/pen/MBoWZJ
这是我的JavaScript代码:
app.controller('myCtrl', function($scope, $mdSidenav) {
});
app.animation('.slide-toggle', ['$animateCss', function($animateCss) {
var lastId = 0;
var _cache = {};
function getId(el) {
var id = el[0].getAttribute("data-slide-toggle");
if (!id) {
id = ++lastId;
el[0].setAttribute("data-slide-toggle", id);
}
return id;
}
function getState(id) {
var state = _cache[id];
if (!state) {
state = {};
_cache[id] = state;
}
return state;
}
function generateRunner(closing, state, animator, element, doneFn) {
return function() {
state.animating = true;
state.animator = animator;
state.doneFn = doneFn;
animator.start().finally(function() {
if (closing && state.doneFn === doneFn) {
element[0].style.height = '';
}
state.animating = false;
state.animator = undefined;
state.doneFn();
});
}
}
return {
addClass: function(element, className, doneFn) {
if (className == 'ng-hide') {
var state = getState(getId(element));
var height = (state.animating && state.height) ?
state.height : element[0].offsetHeight;
var animator = $animateCss(element, {
from: {height: height + 'px'},
to: {height: '0px'}
});
if (animator) {
if (state.animating) {
state.doneFn =
generateRunner(true,
state,
animator,
element,
doneFn);
return state.animator.end();
}
else {
state.height = height;
return generateRunner(true,
state,
animator,
element,
doneFn)();
}
}
}
doneFn();
},
removeClass: function(element, className, doneFn) {
if (className == 'ng-hide') {
var state = getState(getId(element));
var height = (state.animating && state.height) ?
state.height : element[0].offsetHeight;
var animator = $animateCss(element, {
from: {height: '0px'},
to: {height: height + 'px'}
});
if (animator) {
if (state.animating) {
state.doneFn = generateRunner(false,
state,
animator,
element,
doneFn);
return state.animator.end();
}
else {
state.height = height;
return generateRunner(false,
state,
animator,
element,
doneFn)();
}
}
}
doneFn();
}
};
}]);
(function() {
var app = angular.module('app', ['ngAnimate']);
app.animation('.slide-toggle', ['$animateCss', function($animateCss) {
return {
addClass: function(element, className, doneFn) {
if (className == 'ng-hide') {
var animator = $animateCss(element, {
to: {height: '0px'}
});
if (animator) {
return animator.start().finally(function() {
element[0].style.height = '';
doneFn();
});
}
}
doneFn();
},
removeClass: function(element, className, doneFn) {
if (className == 'ng-hide') {
var height = element[0].offsetHeight;
var animator = $animateCss(element, {
from: {height: '0px'},
to: {height: height + 'px'}
});
if (animator) {
return animator.start().finally(doneFn);
}
}
doneFn();
}
};
}]);
})();
这是HTML-一个简单的:
<ul class="menu-toggle-list">
<li><a md-ink-ripple ng-init="showMenu2 = true" ng-click="showMenu2 = !showMenu2" class="md-button menuSub">Tours</a>
<ul ng-show="showMenu2" class="slide-toggle">
<li><a md-ink-ripple ng-init="showMenu3 = true" ng-click="showMenu3 = !showMenu3" class="md-button menuSub">Group tours</a>
<ul ng-show="showMenu3" class="slide-toggle">
<li><a md-ink-ripple href="URL/TOURS/GROUP-TOURS/PLANNING/" class="md-button">Planning</a></li>
<li><a md-ink-ripple href="URL/TOURS/GROUP-TOURS/TYPES/" class="md-button">Types</a></li>
</ul>
</li>
</ul>
</li>
<li><a md-ink-ripple ng-init="showMenu6 = true" ng-click="showMenu6 = !showMenu6" class="md-button menuSub">Users</a>
<ul ng-show="showMenu6" class="slide-toggle">
<li><a md-ink-ripple href="URL/TOURS/GROUP-TOURS/" class="md-button">Staff</a></li>
<li><a md-ink-ripple href="URL/TOURS/GROUP-TOURS/" class="md-button">Clients</a></li>
</ul>
</li>
</ul>
这是CSS:
.slide-toggle {
overflow: hidden;
transition: all 0.25s;
}
答案 0 :(得分:2)
父级在动画之后仍保持高度。您必须将其删除。
动画完成后添加element[0].style.height = 'auto'
。在if后的第36行的generateRunner
函数中
function generateRunner(closing, state, animator, element, doneFn) {
return function () {
state.animating = true;
state.animator = animator;
state.doneFn = doneFn;
animator.start().finally(function () {
if (closing && state.doneFn === doneFn) {
element[0].style.height = '';
}
element[0].style.height = 'auto'; // <----- HERE
state.animating = false;
state.animator = undefined;
state.doneFn();
});
}
}
希望这会有所帮助。祝你好运!