Reputation: 596
I have been trying to Dynamically Animate a background between 2 colours on click using AngularJs Directives and Controllers (with no success so far).
Does anyone know how to do this? I have been trying to store the currCol (stored as an array var pushed into ng-class), but then I can not retrieve the value from ng-class and use it.
I have css transitioned from clear to the colour, but I cannot figure out how to get the transition between 2 dynamic colour vars. Any help will be great. Thanks.
Some code so far:
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>
controllers.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];
}
}
}])
.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;
});
};
}]);
app.css
.greenBg {
transition: background-color 1s ease;
background-color: #00ff00;
}
.redBg {
transition: background-color 1s ease;
background-color: #ff0000;
}
Upvotes: 2
Views: 7267
Reputation: 2113
You don't need a directive for that, just use ngClass
animation hooks.
.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;
}
Upvotes: 5