gpt4 book ai didi

javascript - AngularJS - 找到折叠动画的结尾

转载 作者:行者123 更新时间:2023-11-28 01:40:45 25 4
gpt4 key购买 nike

我有一个关于 angularui bootstrap 的简单问题的 collapse指示。我有一个 <select>菜单。当某人更改菜单时,内容也会更改。但是,我想在应用这些更改之前添加一个动画。具体来说,我希望在显示新内容之前折叠和展开该部分。我可以折叠它,但我想知道如何检测折叠完成的时间,以便我可以展开它。

我可以使用 $timeout我目前使用的方法,但感觉“hackish”和“不正确”,因为如果折叠动画的时间发生变化,那么我必须再次更改时间。

相关代码:

.directive("myBufferAnimation", function($timeout) {
var ignoreFirst = true;
return function(scope, element, attrs) {
scope.$watch("selectBuffer", function(newValue) {
if (ignoreFirst) {
ignoreFirst = false;
return;
}


scope.toCollapse = true;
// Detect end of animation here?
// Bogus solution, as this just waits for one second rather than listening for the end of animation
$timeout(function() {
scope.selected = newValue;
scope.toCollapse = false;
}, 1000);
});
}
});

jsfiddle说明问题。

最佳答案

查看 angularui bootstrap 的折叠指令,它使用 $transition 服务执行动画,但它不会暴露以 Hook 服务返回的 promise 。但是,它确实会更改其上具有 collapse 指令的元素的类名。您可以通过监视元素的类属性来使用它来确定折叠动画何时完成。

下面是监视元素类的代码的工作示例。我做了一个更改,使用 ng-change 来执行动画,而不是观看 selectBuffer。这消除了对 ignoreFirst 变量的需要。

angular.module("testApp", ["ui.bootstrap"]).controller("TestCtrl", function($scope) {
// This would actually be fetched from an ajax call
$scope.data = [{
"title": "Title 1",
"content": "Content 1"
}, {
"title": "The Second Title",
"content": "Some content goes here"
}, {
"title": "Title 3",
"content": "Content 3"
}];
$scope.selected = $scope.data[0];
$scope.selectBuffer = $scope.data[0];
$scope.toCollapse = false;
}).directive("myBufferAnimation", function($timeout) {
return function(scope, element, attrs) {
scope.valueChanged = function() {
scope.toCollapse = true;
// save off the unregister method returned from $watch call.
var unregisterWatch = scope.$watch(function() {
return element.attr('class');
}, function(newClass, oldClass) {
// If the newClass is 'collapse' and the oldClass is 'collapsing'
// this means that the collapsing animation has completed.
if (newClass.indexOf("collapse") !== -1 && oldClass.indexOf("collapsing") !== -1) {
scope.selected = scope.selectBuffer;
scope.toCollapse = false;
// unregister the $watch on the class attribute
// since it is no longer needed.
unregisterWatch();
}
});
};
}
});
<!DOCTYPE html>
<html>

<head>
<link data-require="bootstrap-css@3.1.1" data-semver="3.1.1" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
<script src="https://code.angularjs.org/1.2.26/angular.js"></script>
<script data-require="jquery@*" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script data-require="bootstrap@3.1.1" data-semver="3.1.1" src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script data-require="angular-ui-bootstrap@0.11.0" data-semver="0.11.0" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.min.js"></script>
</head>

<body>
<div ng-app="testApp">
<div ng-controller="TestCtrl">
<select ng-options="opt as opt.title for opt in data" ng-model="selectBuffer" ng-change="valueChanged()"></select>
<br />
<div my-buffer-animation="" collapse="toCollapse" class="test">
<h1>{{selected.title}}</h1>

<p>{{selected.content}}</p>
</div>
</div>
</div>
</body>

</html>

关于javascript - AngularJS - 找到折叠动画的结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26196995/

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