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

标签 javascript angularjs animation gsap

"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>

My transitions are not smooth with this javascript Greensock animation, and I am trying to figure out why? Very reduced problem here with hopefully a straightforward answer. I am calling the addClass and removeClass pieces of the navModalCircleTransition angular animation class , but the transition rendered by Greensock is not smooth. Strange stuff. Everything else is working fine. I have never seen anything like this before. What am I missing?

最佳答案

重新发布作为答案。

来自 TweenMax 的 CSSPlugin 文档:

a common mistake is to forget to use camel case representations of the properties...

在这种情况下,将 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>

希望这有帮助。

关于javascript - Angular 动画类中 Greensock JavaScript 的非平滑补间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36344078/

相关文章:

javascript - 图像未使用 AngularJs 在 prawn pdf 中呈现

javascript - AngularJS 数据与作用域函数的绑定(bind)

java - 如何绘制扩展 MouseInputAdapter 的监听器?

javascript - 运行 Node.js 服务器时无法获取/test.html

javascript - 复选框一次检查一个不起作用

Javascript 聚焦一个带有固定标题的大 div

c# - 在 StackPanel 中为子级设置动画 - WPF (C#)

javascript - 当应用程序使用jquery mobile在cordova应用程序单页中加载页面时触发事件的最佳方式?

css - 更改在angularjs中未选中的已选中行的背景颜色

ios - 通过导航 Controller 使用自定义动画转换到 SecondViewController 中的非事件 View 后