forked from CodeHunger/Jarallax
/
jarallax_animation.js
124 lines (104 loc) · 3.84 KB
/
jarallax_animation.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
////////////////////////////////////////////////////////////////////////////////
// Jarallax animation class ////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
JarallaxAnimation = function (selector, startValues, endValues, jarallax) {
this.progress = -1;
this.selector = selector;
this.startValues = startValues;
this.endValues = endValues;
this.jarallax = jarallax;
};
JarallaxAnimation.prototype.activate = function (progress) {
if (this.progress != progress) {
var start;
var end;
var style;
if (this.startValues.style === undefined) {
style = {easing:'linear'};
} else{
style = this.startValues.style;
}
if (this.startValues.progress.indexOf('%') >= 0) {
start = parseInt(this.startValues.progress,10) / 100;
} else if (JarallaxTools.hasNumbers(this.startValues.progress)) {
start = parseInt(this.startValues.progress,10) / this.jarallax.maxProgress;
}
if (this.endValues.progress.indexOf('%') >= 0)
{
end = parseInt(this.endValues.progress,10) / 100;
} else if (JarallaxTools.hasNumbers(this.endValues.progress)) {
end = parseInt(this.endValues.progress,10) / this.jarallax.maxProgress;
}
if (this.startValues.event) {
this.dispatchEvent(this.progress, progress, start, end);
}
if (progress >= start && progress <= end ) {
for(var i in this.startValues) {
if (i !== 'progress' && i !== 'style' && i !== 'event') {
if (undefined !== this.endValues[i] && i !== 'display' && i !== 'backgroundImage') {
var astart = JarallaxTools.getValues(this.startValues[i]+'');
var aend = JarallaxTools.getValues(this.endValues[i]+'');
var fullresult = '';
for (var j=0; j<astart.length; j++) {
var startValue = astart[j].value;
var endValue = aend[j].value;
var duration = end - start;
var currentTime = (progress-start);
var changeInValue = endValue - startValue;
var result = Jarallax.EASING[style.easing](currentTime, startValue , changeInValue, duration, style.power);
if(astart[j].unit == 'px'){
result = parseInt(result, 10);
}
if(astart[j].unit !== '.'){
result+= astart[j].unit;
}
fullresult += result + ' ';
}
jQuery(this.selector).css(i,fullresult);
} else {
jQuery(this.selector).css(i,this.startValues[i]);
}
}
}
}
this.progress = progress;
}
};
JarallaxAnimation.prototype.dispatchEvent = function(progressOld, progressNew,
start, end) {
var events = this.startValues.event;
var eventData = {};
eventData.animation = this;
eventData.selector = this.selector;
if (progressNew >= start && progressNew <= end ) {
if (events.start && progressOld < start) {
eventData.type = 'start';
events.start(eventData);
}
if (events.start && progressOld > end) {
eventData.type = 'rewind';
events.start(eventData);
}
if (events.animating) {
eventData.type = 'animating';
events.animating(eventData);
}
if (events.forward && progressOld < progressNew) {
eventData.type = 'forward';
events.forward(eventData);
}
if (events.reverse && progressOld > progressNew) {
eventData.type = 'reverse';
events.reverse(eventData);
}
} else {
if (events.complete && progressOld < end && progressNew > end) {
eventData.type = 'complete';
events.complete(eventData);
}
if (events.rewinded && progressOld > start && progressNew < start) {
eventData.type = 'rewind';
events.rewinded(eventData);
}
}
};