-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
42 lines (34 loc) · 1.04 KB
/
script.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
const headerTags = document.querySelectorAll("h1, h2")
const runRandom = tag => {
const originalContent = tag.dataset.original
let newContent = " "
let num = 0
const randomList = "abcdefghijklmnopqrstuvwxyz._$£0!".split("")
const addInterval = setInterval(() => {
num = num + 1
newContent = originalContent.substring(0, num)
if(originalContent == tag.innerHTML) {
clearInterval(addInterval)
clearInterval(randomInterval)
}
}, 100)
const randomInterval = setInterval (() => {
tag.innerHTML = newContent
for (let i = newContent.length; i < originalContent.length; i = i + 1)
tag.innerHTML = tag.innerHTML + randomList[Math.floor(Math.random() * randomList.length)]
}, 50)
}
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if(entry.intersectionRatio > 0.5){
runRandom(entry.target)
}
})
}, {
threshold: [0, 0.5, 1]
} )
//run on page load
headerTags.forEach(tag => {
tag.dataset.original = tag.innerHTML
observer.observe(tag)
})