gpt4 book ai didi

angularjs - Angular JS - 我如何在模型更改时制作动画?

转载 作者:行者123 更新时间:2023-12-03 12:16:20 25 4
gpt4 key购买 nike

当 currentVertical 发生变化时,我正在尝试做一个很好的淡出+淡入过渡。
在淘汰赛中它是如此简单,但我无法在这里弄清楚。请帮忙。

以下代码显示了一个 UL 列表,该列表“绑定(bind)”到 $scope.currentVertical 中的定价数组,当单击 LI 元素时,$scope.currentVertical 会更改,并且 UL 列表会相应更新。这工作正常,但我希望整个#container div 在 $scope.currentVertical 更改时淡出和淡入。请帮忙...

我的html:







<body>
<h1>Pricing Poll</h1>
<div ng-controller="VerticalsController">
<div id="container">
<h2>{{currentVertical.title}}</h2>

<ul>
<li ng-repeat="pricing in currentVertical.pricings">
<a ng-click="currentVertical.selectPricing(pricing)">{{pricing.name}}</a>
</li>
</ul>
</div>
</div>
</body>

我的JavaScript:
function VerticalsController($scope) {

$scope.verticals = [
{
title:'internet',
pricings: [
{
name: 'netvision',
monthly: 23
},
{
name: 'hot',
monthly: 33
},
{
name: '012',
monthly: 28
}
]
},
{
title:'cellular',
pricings: [
{
name: 'cellcom',
monthly: 20
},
{
name: 'pelephone',
monthly: 30
},
{
name: 'orange',
monthly: 25
}
]
},
{
title:'banks',
pricings: [
{
name: 'leumi',
monthly: 20
},
{
name: 'poalim',
monthly: 30
},
{
name: 'benleumi',
monthly: 25
}
]
}];

$scope.selected = [
];

$scope.currentIndex = 0;
$scope.currentVertical = $scope.verticals[0];

$scope.selectPricing = function(pricing) {
$scope.selected.push(pricing);
$scope.currentIndex++;
$scope.currentVertical = $scope.verticals[$scope.currentIndex];
};

/*$scope.remaining = function() {
var count = 0;
angular.forEach($scope.todos, function(todo) {
count += todo.done ? 0 : 1;
});
return count;
};*/
}

最佳答案

您必须使用自定义或创建指令来启动高级 DOM 操作,如动画。

这是您请求的动画的 fiddle ,我使用 visible范围上的变量以触发淡出,并且 $timeout 服务仅在淡出时更改 selectedItem,可以改进以将超时和回调作为指令选项传递...

fiddle :http://jsfiddle.net/g/Bs66R/1/

JS:

function VerticalsController($scope, $timeout) {

$scope.verticals = [{
title:'internet',
pricings: [{
name: 'netvision',
monthly: 23
},
{
name: 'hot',
monthly: 33
},
{
name: '012',
monthly: 28
}]
},
{
title:'cellular',
pricings: [{
name: 'cellcom',
monthly: 20
},
{
name: 'pelephone',
monthly: 30
},
{
name: 'orange',
monthly: 25
}]
},
{
title:'banks',
pricings: [{
name: 'leumi',
monthly: 20
},
{
name: 'poalim',
monthly: 30
},
{
name: 'benleumi',
monthly: 25
}]
}];

$scope.selected = [
];

$scope.currentIndex = 0;
$scope.currentVertical = $scope.verticals[0];

$scope.selectPricing = function(pricing) {
$scope.selected.push(pricing);
$scope.currentIndex++;
$scope.visible = false;

$timeout(function(){
$scope.currentVertical = $scope.verticals[$scope.currentIndex];
$scope.visible = true;
}, 1000);
};

$scope.visible = true;
}

var fadeToggleDirective = function() {
return {
link: function(scope, element, attrs) {
scope.$watch(attrs.uiFadeToggle, function(val, oldVal) {
if(val === oldVal) return; // Skip inital call
// console.log('change');
element[val ? 'fadeIn' : 'fadeOut'](1000);
});
}
}
}

angular.module('app', []).controller('VerticalsController', VerticalsController).directive('uiFadeToggle', fadeToggleDirective);

angular.bootstrap(document.body, ['app']); angular.bootstrap(document.body, ['app']);

HTML:
<h1>Pricing Poll</h1>
<div ng-controller="VerticalsController">
<div id="container" ui-fade-toggle="visible">
<h2>{{currentVertical.title}}</h2>

<ul>
<li ng-repeat="pricing in currentVertical.pricings">
<a ng-click="selectPricing(pricing)">{{pricing.name}}</a>
</li>
</ul>
</div>
</div>

关于angularjs - Angular JS - 我如何在模型更改时制作动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14775751/

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