-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresize.animator.js
62 lines (57 loc) · 2.67 KB
/
resize.animator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
app.directive('resizeAnimator', function($animate) {
return {
link: function(scope, elem, attrs) {
/* control variables to lock animation */
scope.animating = {
width: false,
height: false
};
function resize(newValue, oldValue, property) {
/* animate if size has actually changed and animation isn't already in progress */
if (newValue !== oldValue && !scope.animating[property]) {
var oldValueCss = oldValue + 'px';
var newValueCss = newValue + 'px';
var from = {};
var to = {};
from[property] = oldValueCss;
to[property] = newValueCss;
/* lock down animation */
scope.animating[property] = true;
$animate.animate(elem, from, to, attrs.resizeAnimate).then(function() {
/* animation complete, set property back to auto
this will trigger a recalculation essentially negating our animation
(ie container started at 200px, we animated it to 500px,
but switching it to auto brings it back to 200px)
hence the reason for the control variables */
elem.attr('style', property + ': auto;');
/* since we indirectly modified element width/height,
we need to trigger the rest of the DOM render so the size calculation
is brought back up to its true size. */
scope.$apply();
/* at this point, two digest cycles would have been triggered,
using control variables allows us to ignore unwanted animations */
scope.animating[property] = false;
});
}
}
function resizeWidth(newValue, oldValue) {
/* don't animate if other axis is */
if (!scope.animating.height) {
resize(newValue, oldValue, 'width');
}
}
function resizeHeight(newValue, oldValue) {
/* don't animate if other axis is */
if (!scope.animating.width) {
resize(newValue, oldValue, 'height');
}
}
scope.$watch(function() {
return elem[0].offsetWidth;
}, resizeWidth);
scope.$watch(function() {
return elem[0].offsetHeight;
}, resizeHeight);
}
};
});