AngularJs设置背景颜色/颜色的动画

AngularJs animate background color / colour

本文关键字:颜色 动画 AngularJs 背景 设置      更新时间:2024-06-30

我一直在尝试使用AngularJs指令和控制器在点击时动态制作2种颜色之间的背景动画(到目前为止没有成功)。

有人知道怎么做吗?我一直在尝试存储currCol(存储为推入ng类的数组var),但之后我无法从ng类中检索值并使用它。

我已经将css从clear转换为color,但我不知道如何在2个动态颜色变量之间进行转换。任何帮助都会很棒。谢谢

到目前为止的一些代码:

index.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>

控制器.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];
        }
    }
}])

.方向.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;        
    });
  };  
}]);

app.css

.greenBg {
    transition: background-color 1s ease;
    background-color: #00ff00;
}
.redBg {
    transition: background-color 1s ease;
    background-color: #ff0000;
}

不需要指令,只需使用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;
}

演示