20

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

以下代码显示一个 UL 列表,当单击 LI 元素时,该 UL 列表“绑定”到 $scope.currentVertical 中的定价数组,$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;
  };*/
}
4

2 回答 2

9

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

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

小提琴: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>
于 2013-02-08T16:14:22.727 回答
9

我建议您使用AngularJS Core 中提供的新ngAnimate指令。

在这里阅读更多

于 2013-04-04T18:30:01.283 回答