AngularJs 动画背景颜色/颜色

cuz*_*ter 2 javascript directive colors angularjs jquery-animate

我一直在尝试使用 AngularJs 指令和控制器在单击时动态设置两种颜色之间的背景动画(到目前为止尚未成功)。

有谁知道如何做到这一点?我一直在尝试存储 currCol (存储为数组 var 推入 ng-class),但后来我无法从 ng-class 检索该值并使用它。

我已将 css 从透明转换为颜色,但我无法弄清楚如何在两个动态颜色变量之间进行转换。任何帮助都会很棒。谢谢。

到目前为止的一些代码:

索引.html

    <body ng-app="colourTest">
        <div ng-controller="BgCtrl" >
            <ion-nav-bar id="bgCont" data-mylo-color="" ng-class="colorVal" animate-On-Change="colorVal" ></ion-nav-bar>
            <ion-nav-view animation="slide-left-right">
                 <button ng-click="test3()">set bg</button>           
            </ion-nav-view>
        </div>
    </body>
Run Code Online (Sandbox Code Playgroud)

控制器.js

.controller('BgCtrl', ['$scope', function ($scope) {
    $scope.colorPane = 'whiteBg';
    $scope.colorVal = '';
    var colCount = 0;
    var colorArr = ['redBg', 'greenBg', 'blueBg', 'whiteBg'];
    var currColor = '';       

    $scope.test3 = function () {         
        if (colCount < colorArr.length)
        {
            $scope.colorVal = colorArr[colCount];            
            colCount ++;    
        } else {
            colCount = 0;
            $scope.colorVal = colorArr[colCount];
        }
    }
}])
Run Code Online (Sandbox Code Playgroud)

.directives.js

.directive('animateOnChange', ['$animate', function($animate) {  

  return function (scope, elem, attrs) {
    //var container = angular.element(document.querySelector('#bgCont') );    
    //var currCol = container[0].attributes[1];
    scope.$watch(attrs.animateOnChange, function (nv, ov) {

        //var colVar = attrs.ngClass;
        //colVar = nv;        
    });
  };  
}]);
Run Code Online (Sandbox Code Playgroud)

应用程序.css

.greenBg {
    transition: background-color 1s ease;
    background-color: #00ff00;
}
.redBg {
    transition: background-color 1s ease;
    background-color: #ff0000;
}
Run Code Online (Sandbox Code Playgroud)

Mam*_*ter 5

您不需要为此指定指令,只需使用ngClass动画挂钩即可。

.greenBg-add, .greenBg-remove,
.redBg-add, .redBg-remove,
.blueBg-add, .blueBg-remove,
.whiteBg-add, .whiteBg-remove {
  transition: background-color 1000ms linear;
}

.greenBg,
.greenBg-add.greenBg-add-active {
    background-color: #00ff00;
}
.redBg,
.redBg-add.redBg-add-active {
    background-color: #ff0000;
}
.blueBg,
.blueBg-add.blueBg-add-active {
    background-color: #0000ff;
}
.whiteBg,
.whiteBg-add.whiteBg-add-active {
    background-color: #ffffff;
}
Run Code Online (Sandbox Code Playgroud)

演示