AngularJS - div animation using a variable sphere variable

I am new to angular. I want to animate a div body using ng-click on an ng-repeat element. This is what I have tried so far.

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;
  }

});

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>

What I wanted to do was add a transition effect to the fading out of the item-body element as a changing element. I searched on the Internet, but I can not find a solution. Please, help.

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

+4
source share
3 answers

Animation of the elements themselves

, 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;
  }

});
.item-body {
  color: red;
}
.item {
  cursor: pointer;
  transition: all 250ms linear;
}
.item.selected {
  cursor: default;
  background-color: blue;
  color: white;
}
<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>

item-body

item-body , - .

, , , (, this).

:

  • , item-body ,
  • , 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);

  }

});
.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;
}
<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>
+6

ng-, , :

$scope.selectedStyle = false;

<tr ng-class="({'myClass':selectedStyle})" >
+1

, body-body angular.

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" my-dir ng-model="myValue">
    {{selectedItem.name}}
  </div>
</div>

Remember to download angular -animate.js and add it to your solution. The important part is to add the dependency to 'ngAnimate' in your module and add your custom directive as follows.

Before that, add a class style.

 .myClass
    {
         background-color: red;
          transition: all 1s;
          -webkit-transition: all 1s ease-in-out;
    }

Note that I use the $ watch method to track a variable

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

        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;
                $scope.myValue = item.name;

            }

        }).directive("myDir", function ($animate, $timeout) {
            return function (scope, element, attrs) {
                scope.$watch('myValue', function (newValue, oldValue) {

                    if (newValue != oldValue) {

                        $animate.addClass(element, "myClass").then(function () {
                            $timeout(function () { $animate.removeClass(element, "myClass"); });
                        });
                    }

                },true);
            }
        }); 
+1
source

Source: https://habr.com/ru/post/1662815/


All Articles