gpt4 book ai didi

javascript - Angular Accordion 不关闭

转载 作者:太空宇宙 更新时间:2023-11-04 11:17:15 24 4
gpt4 key购买 nike

我有一个简单的基于 Angularjs 的 Accordion ,它在过去的元素中运行良好(使用 <div> 而不是现在使用 <li> )。 Accordion 打开/向下滑动,但不会再次向上滑动。

这是一个傻瓜:http://plnkr.co/edit/rIKIwYLowORhag7qu9Fy?p=preview

点击 plunker 上的 + 展开。

JS:

app.directive('sliderContentDirective', function () {
return {
restrict:'A',
compile: function (element, attr) {
// wrap tag
var contents = element.html();
element.html('<li class="slideable_content" style="margin:0 !important; padding:0 !important" >' + contents + '</li>');
//element.html('<tr class="slideable_content" style="margin:0 !important; padding:0 !important" >' + contents + '</tr>');

return function postLink(scope, element, attrs) {
// default properties
attrs.duration = (!attrs.duration) ? '0.7s' : attrs.duration;
attrs.easing = (!attrs.easing) ? 'ease-in-out' : attrs.easing;
element.css({
'overflow': 'hidden',
'height': '0px',
'transitionProperty': 'height',
'transitionDuration': attrs.duration,
'transitionTimingFunction': attrs.easing
});
};
}
};
});

app.directive('sliderToggleDirective', function($document, $timeout, $animate) {
return {
restrict: 'AE',
scope: {
target: "@"
},
link: function(scope, element, attrs) {

var timeoutFunc = function () {
var target = angular.element($document[0].querySelector("#" + scope.target))[0];

attrs.expanded = false;
element.bind('click', function() {
var content = target.querySelector('.slideable_content');
var y = content.clientHeight;
target.style.height = y + 'px';
if(!attrs.expanded) {
content.style.border = '1px solid rgba(0,0,0,0)';
content.style.border = 0;
}
else {
$animate.removeClass(angular.element(target), 'auto', function(){$timeout(closeAccordionFunc);});
}

attrs.expanded = !attrs.expanded;
if (attrs.expanded) {
$timeout(adjustHeightFunc, 800);
}
});
}

var adjustHeightFunc = function() {
var target = angular.element($document[0].querySelector("#" + scope.target))[0];
var content = target.querySelector('.slideable_content');
$animate.addClass(angular.element(target), 'auto');
}

var closeAccordionFunc = function() {
var target = angular.element($document[0].querySelector("#" + scope.target))[0];
target.style.height = '0px';
}

$timeout(timeoutFunc, 0);
}
}
});

最佳答案

一开始并不确定为什么该函数在那里,但通过将其移出使其正常工作。

这一行:

$animate.removeClass(angular.element(target), 'auto', function(){$timeout(closeAccordionFunc);});

到这一行:

$animate.removeClass(angular.element(target), 'auto');
$timeout(closeAccordionFunc);

关于javascript - Angular Accordion 不关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33103028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com