AngularJS - 使用更改范围变量设置动画div

lps*_*wan 5 html javascript css angularjs

我很有棱角.我想使用ng-click元素在ng-repeat元素上设置div主体的动画.这是我到目前为止所尝试的.

app.js

var app = angular.module( 'app', [] );

app.controller('appController', function($scope) {

    $scope.items = [
  {"id": "id1", "name": "Name 1"},
  {"id": "id2", "name": "Name 2"},
  {"id": "id3", "name": "Name 3"}
  ];

  $scope.selectedStyle = {"background-color": "blue", "color": "white"};
  $scope.selectedItem = $scope.items[0];

  $scope.selectItem = function(item) {
    $scope.selectedItem = item;
  }

});
Run Code Online (Sandbox Code Playgroud)

app.html

<div ng-app="app" ng-controller="appController">
  <table class=table>
    <tbody>
      <tr ng-repeat="item in items" ng-click="selectItem(item)" ng-style="item.id === selectedItem.id && selectedStyle">
        <td>
          {{item.id}}
        </td>
      </tr>
    </tbody>
  </table>

  <div class="item-body">
    {{selectedItem.name}}
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我想要做的是将item-body div的淡入过渡效果添加为更改项.我在网上搜索,但似乎无法找到解决方案.请帮忙.

JSFiddle - https://jsfiddle.net/lpsandaruwan/ot45qdno/14/

Tha*_*guy 6

动画物品本身

您可以通过使用angular向所选元素添加类,并使用css过渡管理过渡来完成此操作.

这样就没有必要了$scope.selectedStyle.我们将在css中管理它.

所以,流程将如下:

  1. 当用户单击时,angular将为selected单击的元素添加一个类.
  2. CSS转换为类item将处理颜色的变化对你(无论是选择和取消).

这是代码:

var app = angular.module('app', []);

app.controller('appController', function($scope) {

  $scope.items = [{
    "id": "id1",
    "name": "Name 1"
  }, {
    "id": "id2",
    "name": "Name 2"
  }, {
    "id": "id3",
    "name": "Name 3"
  }];

  $scope.selectedItem = $scope.items[0];

  $scope.selectItem = function(item) {
    $scope.selectedItem = item;
  }

});
Run Code Online (Sandbox Code Playgroud)
.item-body {
  color: red;
}
.item {
  cursor: pointer;
  transition: all 250ms linear;
}
.item.selected {
  cursor: default;
  background-color: blue;
  color: white;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="appController">
  <table class=table>
    <tbody>
      <tr ng-repeat="item in items" ng-click="selectItem(item)" class="item" ng-class="{ 'selected': selectedItem === item }">
        <td>
          {{item.id}}
        </td>
      </tr>
    </tbody>
  </table>

  <div class="item-body">
    {{selectedItem.name}}
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

动画片 item-body

如果要为item-bodyon更改设置动画,可以使用简单的超时来添加和删除类.

此外,您应该知道有一些模块可以用来实现这一点(像这样).

这是我的建议:

  1. 添加一个标志,让item-body元素知道它需要隐藏和显示
  2. 将该标志挂钩到一个类
  3. 使该标志隐藏并显示该元素,类似于transition我们之前所做的

var app = angular.module('app', []);

app.controller('appController', function($scope, $timeout) {

  $scope.items = [{
    "id": "id1",
    "name": "Name 1"
  }, {
    "id": "id2",
    "name": "Name 2"
  }, {
    "id": "id3",
    "name": "Name 3"
  }];

  $scope.selectedItem = $scope.items[0];

  $scope.selectItem = function(item) {
    $scope.changeIsOn = true;
    $timeout(function() {
      $scope.selectedItem = item;
      $scope.changeIsOn = false;
    }, 250);

  }

});
Run Code Online (Sandbox Code Playgroud)
.item-body {
  color: red;
  transition: opacity 250ms linear;
}
.item-body.changing {
  opacity: 0;
}
.item {
  cursor: pointer;
  transition: all 250ms linear;
}
.item.selected {
  cursor: default;
  background-color: blue;
  color: white;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="appController">
  <table class=table>
    <tbody>
      <tr ng-repeat="item in items" ng-click="selectItem(item)" class="item" ng-class="{ 'selected': selectedItem === item }">
        <td>
          {{item.id}}
        </td>
      </tr>
    </tbody>
  </table>

  <div class="item-body" ng-class="{ 'changing': changeIsOn }">
    {{selectedItem.name}}
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 选择这一个作为答案,因为它很好地解释和整洁.:-) (2认同)