-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
151 lines (144 loc) · 4.49 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
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
/*!
* vue-animatecss-mixin - 1.0.2
* (c) 2020 srxboys
* Released under the MIT License.
* https://github.com/cheere/vue-animatecss-mixin
*/
/**
* Animate.CSS
* https://github.com/animate-css/animate.css
*/
const AnimateCssMixin = {
methods: {
/**
* Monitor the animation finish.
* 动画对象 添加 监听 动画播放完毕
* @param {object} target animation object(动画对象)
*/
animateAddEndListener(target) {
target.addEventListener('animationend', () => {
const classList = target.classList || []
const newList = []
for (let i = 0; i < classList.length; i++) {
const name = classList[i]
if (!name) {
continue
} else if (name.length > 6 && name.substring(0, 7) === 'animate') {
continue
}
newList.push(name)
}
target.classList = newList
target.classList.value = newList.join(' ')
})
},
/**
* Remove monitor animation, play finished
* 移除监听
* @param {object} targets animation objects(动画对象)
*/
animateRemoveAllListener(targets) {
if (!targets || !Array.isArray(targets)) {
return
}
targets.forEach(element => {
element.removeEventListener('animationend')
});
},
animateRemoveListener(target) {
target.removeEventListener('animationend')
},
/**
* loop get DOM.node
* @param {object} target animation object(动画对象)
* @param {Function} callback
*/
animateChildNodes(target, callback) {
const elChilds = target.childNodes
if (elChilds && elChilds.length) {
for (let i = 0; i < elChilds.length; i++) {
const element = elChilds[i]
if (element && element.nodeType === 1) {
if (callback && typeof callback === 'function') {
callback(element)
}
}
}
}
},
/**
* loop get animate object
* @param {object} target animation object(动画对象)
* @param {Array} array
*/
animateForElement(target, array) {
this.animateChildNodes(target, e => {
const animation = e.getAttribute('data-animation')
if (animation && typeof animation === 'string') {
array.push(e)
}
if (e.nodeType === 1 && e.childElementCount > 0) {
this.animateForElement(e, array)
}
})
},
/**
* get animation object
* 获取对象(id 、 class、ref)
* @param {Any} target animation object(动画对象)
*/
animateGetTarget(target) {
if (!target) {
throw new TypeError('animateGetTarget target = null')
} else if (typeof target === 'string') {
const prefix = target.substring(0, 1)
if (prefix === '#') {
const id = target.slice(1, target.length)
const e = document.getElementById(id)
return e
} else {
if (prefix !== '.') {
target = '.' + target
}
const className = target.slice(1, target.length)
const e = document.getElementsByClassName(className)
return e
}
} else if (typeof target === 'object') {
if (target.nodeType === 1) {
return target
} else {
throw new TypeError('animateGetTarget target.nodeType !== 1')
}
} else {
throw new TypeError('animateGetTarget target allow string/object')
}
},
/**
* play animation
* @param {Array<string>} targets animation Array(动画对象数组)
*/
animatePlayground(targets = []) {
for (let i = 0; i < targets.length; i++) {
const itemTarget = targets[i]
const itemTargetEl = this.animateGetTarget(itemTarget)
if (itemTargetEl && itemTargetEl.nodeType === 1) {
const animation = `${itemTargetEl.getAttribute('data-animation')}`
if (animation && animation !== 'null') {
this.animateAddEndListener(itemTargetEl)
const animationClassList = animation.split(' ')
itemTargetEl.classList.add('animate__animated')
for (const j in animationClassList) {
const aniClassName = animationClassList[j]
if (aniClassName && typeof aniClassName === 'string' && aniClassName.length) {
const cn = 'animate__' + aniClassName
itemTargetEl.classList.add(cn)
}
}
}
}
}
},
}
}
export default AnimateCssMixin