-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (36 loc) · 1.19 KB
/
index.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
var className = 'animation';
function parallel(...targets) {
return flow('all', targets);
}
function race(...targets) {
return flow('race', targets);
}
function flow(type, targets) {
targets = targets.map(item => getElement(item)).reduce((x, y) => x.concat(y));
var promises = targets.map(el => new Promise(resolve => el.addEventListener('transitionend', resolve)));
targets.forEach(el => el.classList.add(className));
return Promise[type](promises);
}
function series(...targets) {
targets = targets.map(item => getElement(item)).reduce((x, y) => x.concat(y));
var promises = targets.map(el => new Promise(resolve => el.addEventListener('transitionend', resolve)));
for(let i = 0, length = promises.length; i < length - 1; i++) {
promises[i].then(() => targets[i + 1].classList.add(className));
}
targets[0].classList.add(className);
return Promise.all(promises);
}
function getElement(selector) {
if(typeof selector === 'string') {
return Array.from(document.querySelectorAll(selector));
}
if(selector.length >= 0) {
return Array.from(selector);
}
return selector;
}
export default {
parallel,
race,
series
};