Angular动画类中Greensock JavaScript的非平滑补间

时间:2016-03-31 21:23:59

标签: javascript angularjs animation greensock



"use strict";
angular.module("app", ['ngAnimate'])
  .controller('appCtrl', function($scope, $timeout) {

    $timeout(function() {
      $scope.currentIndex = 0;
    });

    $scope.isCurrentIndex = function(index) {
      return $scope.currentIndex === index;
    }

    $scope.setCurrentIndex = function(index) {
      $scope.currentIndex = index;

    }

  })
  .animation('.navModalCircleTransition', function() {
    return {
      addClass: function(element, className, done) {

        if (className === 'active') {
          TweenMax.set(element, {
            background: "#000000"
          });
          TweenMax.to(element, 1, {
            background: "#C32026",
            onComplete: done
          });

        } else {
          done();
        }
      },
      removeClass: function(element, className, done) {
        if (className === 'active') {
          TweenMax.set(element, {
            background: "#C32026"
          });
          TweenMax.to(element, 1, {
            background: "#000000",
            onComplete: done
          });

        } else {
          done();
        }
      }
    }
  });

.navModalCircleContainer {
  display: flex;
  justify-content: space-between;
  padding: 0 25%;
}
.navModalCircle {
  height: 25px;
  width: 25px;
  border-radius: 100%;
  background: #000000;
}

<div ng-app="app">
  <div ng-controller="appCtrl">

    <div class="navModalCircleContainer">
      <div class="navModalCircle navModalCircleTransition" ng-class="{active: isCurrentIndex(0)}"></div>
      <div class="navModalCircle navModalCircleTransition" ng-class="{active: isCurrentIndex(1)}"></div>
      <div class="navModalCircle navModalCircleTransition" ng-class="{active: isCurrentIndex(2)}"></div>
      <div class="navModalCircle navModalCircleTransition" ng-class="{active: isCurrentIndex(3)}"></div>
      <div class="navModalCircle navModalCircleTransition" ng-class="{active: isCurrentIndex(4)}"></div>
    </div>

    <br />
    <button type="button" ng-click="setCurrentIndex(0)">set currentIndex = 0</button>

    <br />
    <br />


    <button type="button" ng-click="setCurrentIndex(1)">set currentIndex = 1</button>
    <br />
    <br />


    <button type="button" ng-click="setCurrentIndex(2)">set currentIndex = 2</button>
    <br />
    <br />


    <button type="button" ng-click="setCurrentIndex(3)">set currentIndex = 3</button>
    <br />
    <br />


    <button type="button" ng-click="setCurrentIndex(4)">set currentIndex = 4</button>
    <br />
    <br />

  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.2/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular-animate.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/gsap/1.18.2/TweenMax.min.js"></script>
&#13;
&#13;
&#13;

  

这个javascript Greensock动画我的过渡不顺畅,我想弄清楚为什么?这里的问题非常简单,希望是一个直截了当的答案。我正在调用navModalCircleTransition角度动画类的addClass和removeClass片段,但Greensock呈现的过渡并不平滑。奇怪的东西。其他一切都很好。我以前从未见过这样的事。我错过了什么?

1 个答案:

答案 0 :(得分:1)

重新发布作为答案。

来自TweenMax的 CSSPlugin 文档:

  

一个常见的错误是忘记使用属性的驼峰表示......

在这种情况下,将background更改为backgroundColor可以按预期修复问题和动画。

<强> 段:

"use strict";
angular.module("app", ['ngAnimate'])
  .controller('appCtrl', function($scope, $timeout) {
    $timeout(function() {
      $scope.currentIndex = 0;
    });
    $scope.isCurrentIndex = function(index) {
      return $scope.currentIndex === index;
    }
    $scope.setCurrentIndex = function(index) {
      $scope.currentIndex = index;

    }
  })
  .animation('.navModalCircleTransition', function() {
    return {
      addClass: function(element, className, done) {
        if (className === 'active') {
          TweenMax.to(element, 0.4, {
            backgroundColor: '#C32026',
            onComplete: done
          });
        } else {
          done();
        }
      },
      removeClass: function(element, className, done) {
        if (className === 'active') {
          TweenMax.to(element, 0.4, {
            backgroundColor: '#000000',
            onComplete: done
          });
        } else {
          done();
        }
      }
    }
  });
.navModalCircleContainer {
  display: flex;
  justify-content: space-between;
  padding: 0 25%;
}
.navModalCircle {
  height: 25px;
  width: 25px;
  border-radius: 100%;
  background: #000000;
}
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.2/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular-animate.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/gsap/1.18.2/TweenMax.min.js"></script>
<div ng-app="app">
  <div ng-controller="appCtrl">
    <div class="navModalCircleContainer">
      <div class="navModalCircle navModalCircleTransition" ng-class="{active: isCurrentIndex(0)}"></div>
      <div class="navModalCircle navModalCircleTransition" ng-class="{active: isCurrentIndex(1)}"></div>
      <div class="navModalCircle navModalCircleTransition" ng-class="{active: isCurrentIndex(2)}"></div>
      <div class="navModalCircle navModalCircleTransition" ng-class="{active: isCurrentIndex(3)}"></div>
      <div class="navModalCircle navModalCircleTransition" ng-class="{active: isCurrentIndex(4)}"></div>
    </div>
    <br />
    <button type="button" ng-click="setCurrentIndex(0)">set currentIndex = 0</button>
    <br />
    <br />
    <button type="button" ng-click="setCurrentIndex(1)">set currentIndex = 1</button>
    <br />
    <br />
    <button type="button" ng-click="setCurrentIndex(2)">set currentIndex = 2</button>
    <br />
    <br />
    <button type="button" ng-click="setCurrentIndex(3)">set currentIndex = 3</button>
    <br />
    <br />
    <button type="button" ng-click="setCurrentIndex(4)">set currentIndex = 4</button>
    <br />
    <br />
  </div>
</div>

希望这有帮助。