-
Notifications
You must be signed in to change notification settings - Fork 131
/
scrollanim.js
231 lines (195 loc) · 4.93 KB
/
scrollanim.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* CSS3 scroll animation
*
* MIT licensed. By Afshin Mehrabani <[email protected]>
*
* This project is a part of Kissui framework.
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['kissuiPosition'], function (kissuiPosition) {
return (root.kissuiScrollAnim = factory(kissuiPosition));
});
} else {
root.kissuiScrollAnim = factory(root.kissuiPosition);
}
}(this, function (kissuiPosition) {
/**
* options
*/
var _options = {
// trigger the events on module init?
triggerOnInit: true,
// prefix for all `data-...` attributes
attributePrefix: 'data-kui-',
animAttribute: 'anim',
// when to trigger the animation?
eventAttribute: 'event',
// default event to trigger
defaultEvent: 'in',
// reset the animation event after element is out of the viewport?
// enabling this option triggers the event each time it appears in the viewport
autoReset: true
};
/**
* To store all available elements with their options
*/
var _elements = [];
/**
* Get the attribute name
*
*/
function __(name) {
return _options.attributePrefix + name;
};
/**
* Find elements
*/
function _populate () {
//clear old elements first
_elements = [];
var elements = document.querySelectorAll('*[' + __(_options.animAttribute) + ']');
for (var i = 0;i < elements.length;i++) {
var param = {};
var element = elements[i];
var anim = element.getAttribute(__(_options.animAttribute));
var event = element.getAttribute(__(_options.eventAttribute)) || 'in';
param[event] = anim;
_add(element, param);
}
};
/**
* Adds a new item to _elements array
*
* Sample event object:
* {
* 'in': 'fadeIn',
* 'out': 'fadeOut'
* }
*
* See kissui.position for more options to bind events.
*/
function _add (element, event) {
var eventObj = {};
for (var e in event) {
kissuiPosition.add(element, e);
eventObj[e] = {
animation: event[e],
// adding active flag
active: false
};
}
kissuiPosition.add(element, 'out');
// add visibility: hidden to the element
element.style.opacity = '0';
_elements.push({
element: element,
event: eventObj
});
};
/**
* Finds an element by looking into the _elements
*
*/
function _find (element) {
for (var i = 0;i < _elements.length; i++) {
var elx = _elements[i];
if (element === elx.element) {
return elx;
}
}
return null;
};
/**
* Attaching corresponded css3 class to the element
*
*/
function _attach (element, event) {
for (var e in element.event) {
if (e == event && element.event[e].active === false) {
element.element.style.opacity = '1';
element.element.className += ' animated ' + element.event[e].animation;
(function (element, e) {
_addEventListener(element.element, [
'webkitAnimationEnd',
'mozAnimationEnd',
'MSAnimationEnd',
'oanimationend',
'animationend'], function () {
element.element.className = element.element.className.replace('animated ' + element.event[e].animation, '');
//set this flag to prevent processing same element twice
element.event[e].active = true;
});
}(element, e));
}
}
};
/**
* To bind an event to browser
*
*/
function _addEventListener (element, event, fn) {
// is event an array?
if (typeof (event) == 'object') {
for (var i = 0; i < event.length;i++) {
_addEventListener(element, event[i], fn);
}
}
if (element.addEventListener) { // modern browsers including IE9+
element.addEventListener(event, fn, false);
} else if (element.attachEvent) { // IE8 and below
element.attachEvent('on' + event, fn);
}
};
/**
* Clear animation, reset `opacity` and `active` flag on an element
*
*/
function _resetElement (element) {
var elx = _find(element)
for (var e in elx.event) {
elx.event[e].active = false;
}
element.style.opacity = 0;
};
/**
* Set option
*
*/
function _setOption (name, value) {
_options[name] = value;
};
/**
* Set an object of options
*/
function _setOptions (options) {
for (var o in options) {
_setOption(o, options[o]);
}
};
/**
* Start the module
*/
function _init () {
_populate.call(this);
kissuiPosition.on('*', function (element, event) {
_attach(_find(element), event);
});
// to manage `autoReset`
kissuiPosition.on('out', function (element) {
if (_options.autoReset) {
_resetElement(element);
}
});
kissuiPosition.init();
};
_init()
return {
_options: _options,
_elements: _elements,
init: _init,
add: _add,
setOption: _setOption,
setOptions: _setOptions
};
}));