-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSplitTextJS.js
49 lines (49 loc) · 1.72 KB
/
SplitTextJS.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
/**
* Author: Alexandre Chabeau
* License: MIT
* Contact: [email protected]
* Original repos: https://github.com/saucyspray/split-text-js
*/
class SplitTextJS {
constructor(_target) {
this.result = new Object()
this.result.originalText = _target.innerText
this.result.splitted = this.split(_target)
this.result.words = this.result.splitted.querySelectorAll('.SplitTextJS-wrapper')
this.result.chars = this.result.splitted.querySelectorAll('.SplitTextJS-char')
this.result.spaces = this.result.splitted.querySelectorAll('.SplitTextJS-spacer')
return this.result
}
createSpan(_class) {
let span = document.createElement('span')
span.style.display = "inline-block"
span.className = _class
return span
}
split(_target) {
let containerArray = new Array
const splittedTarget = _target.innerText.split(' ')
let counter = splittedTarget.length
splittedTarget.map(word => {
const wrapper = this.createSpan('SplitTextJS-wrapper')
word.split(/(?!^)/).map(char => {
let el = this.createSpan('SplitTextJS-char')
el.innerText = char
wrapper.appendChild(el)
})
counter--
containerArray.push(wrapper)
if (counter > 0) {
let space = this.createSpan('SplitTextJS-char SplitTextJS-spacer')
space.innerHTML = ' '
containerArray.push(space)
}
})
_target.innerHTML = ''
containerArray.forEach(child => {
_target.appendChild(child)
})
return _target
}
}
module.exports = SplitTextJS;