AngularJS Animations का उपयोग UI को interactive, smooth और user-friendly बनाने के लिए किया जाता है। Animations से elements के show, hide, add, remove और state change को visually attractive बनाया जा सकता है। AngularJS animations CSS और JavaScript दोनों के साथ work करती हैं, लेकिन सबसे common और simple तरीका CSS animations का होता है।
AngularJS में animations ngAnimate module के जरिए implement की जाती हैं।
ngAnimate Module क्या है
ngAnimate AngularJS का official animation module है जो AngularJS directives के साथ animations enable करता है।
यह module इन actions पर animation apply करता है:
- Element show / hide
- DOM में element add / remove
- Class add / remove
- Repeat items enter / leave
ngAnimate Module Include करना
AngularJS animations use करने के लिए पहले angular-animate.js include करना ज़रूरी है।
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular-animate.min.js"></script>
var app = angular.module("myApp", ["ngAnimate"]);
Code Explanation
angular-animate.min.jsanimation support देता है- Module dependencies में
"ngAnimate"add किया गया है - इसके बिना AngularJS animations काम नहीं करेंगी
ng-show और ng-hide Animation
सबसे common animation ng-show और ng-hide के साथ use होती है।
<div ng-app="myApp" ng-controller="aniCtrl">
<button ng-click="show = !show">Toggle</button>
<p ng-show="show" class="fade">Hello AngularJS</p>
</div>
app.controller("aniCtrl", function($scope) {
$scope.show = true;
});
.fade.ng-hide {
opacity: 0;
}
.fade {
transition: opacity 0.5s ease;
}
Code Explanation
ng-showelement की visibility control करता हैng-hideclass AngularJS automatically add करता है- CSS
transitionsmooth fade effect देता है
ng-if Animation
ng-if DOM से element को पूरी तरह add और remove करता है।
<button ng-click="visible = !visible">Toggle</button>
<p ng-if="visible" class="slide">Sliding Text</p>
.slide.ng-enter {
opacity: 0;
transform: translateY(-20px);
}
.slide.ng-enter-active {
opacity: 1;
transform: translateY(0);
transition: all 0.4s ease;
}
.slide.ng-leave {
opacity: 1;
}
.slide.ng-leave-active {
opacity: 0;
transition: opacity 0.3s ease;
}
Code Explanation
.ng-enterelement add होते समय apply होती है.ng-leaveelement remove होते समय apply होती हैng-ifreal DOM animation के लिए useful है
ng-repeat Animation
List items add और remove होने पर animation apply की जा सकती है।
<ul>
<li ng-repeat="item in items" class="list">
{{item}}
</li>
</ul>
<button ng-click="add()">Add</button>
<button ng-click="remove()">Remove</button>
$scope.items = ["A", "B", "C"];
$scope.add = function() {
$scope.items.push("New");
};
$scope.remove = function() {
$scope.items.pop();
};
.list.ng-enter {
opacity: 0;
}
.list.ng-enter-active {
opacity: 1;
transition: opacity 0.4s;
}
.list.ng-leave {
opacity: 1;
}
.list.ng-leave-active {
opacity: 0;
transition: opacity 0.4s;
}
Code Explanation
ng-repeatitems enter और leave detect करता है- CSS classes automatically apply होती हैं
- List animation smooth दिखाई देती है
ng-class Animation
Class add या remove होने पर animation trigger होती है।
<button ng-click="active = !active">Toggle</button>
<p ng-class="{highlight: active}" class="box">
Animated Box
</p>
.box {
transition: background-color 0.5s;
}
.highlight {
background-color: yellow;
}
Code Explanation
ng-classclass dynamically add/remove करता है- CSS transition animation provide करता है
- State change animation के लिए useful है
ngAnimate CSS Classes Summary
AngularJS automatically ये classes apply करता है:
ng-enterng-enter-activeng-leaveng-leave-activeng-hide-addng-hide-remove
Explanation
- ये classes animation lifecycle control करती हैं
- Developer सिर्फ CSS लिखता है
- AngularJS timing manage करता है
JavaScript Animations (Advanced)
AngularJS JavaScript-based animations भी support करता है।
app.animation(".fade", function() {
return {
enter: function(element, done) {
element.css("opacity", 0);
element.animate({ opacity: 1 }, done);
}
};
});
Code Explanation
.animation()API custom animation define करता हैenter,leavehooks available होते हैं- Advanced control के लिए use किया जाता है
Performance Tips
- Simple animations के लिए CSS prefer करें
- Heavy animations avoid करें
- Mobile users को ध्यान में रखें
- Unnecessary watchers न बनाएं
AngularJS Animations Best Practices
- Always
ngAnimatemodule include करें - Small और subtle animations use करें
- UX improve करने के लिए animation रखें, distraction के लिए नहीं
- CSS animations को priority दें
Common Animation Mistakes
ngAnimateinclude करना भूल जाना- Too many animations apply करना
ng-ifऔरng-showका difference न समझना- Animation logic controller में लिखना
Summary
इस chapter में आपने सीखा:
- AngularJS animations क्या हैं
ngAnimatemodule का उपयोगng-show,ng-if,ng-repeatanimations- CSS और JavaScript animations
- Animation lifecycle classes
- Best practices और common mistakes
