-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
63 lines (52 loc) · 1.6 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const btnStart = document.querySelector('#start')
, next = document.querySelectorAll('.next')
, previous = document.querySelectorAll('.previous')
, finishBtn = document.querySelectorAll('.finish')
, steps = Array.from(document.querySelectorAll('.step'))
, body = document.querySelector('body')
let iterator
function Iterator(arr){
this.index = -1;
this.next = () => {
return this.index < arr.length
? {value: arr[++this.index], done: false}
: {done: true}
};
this.previous = () => {
return this.index > 0
? {value: arr[--this.index], done: false}
: {done: true}
};
this.actual = () => ({value: arr[this.index], done: false})
this.reset = () => { this.index = -1 };
}
function finish() {
iterator.reset()
steps.forEach(s => s.classList.remove('showing'))
body.classList.remove('ontour')
}
function toggleStep(active, inactive) {
active && active.classList.remove('showing')
inactive && inactive.classList.add('showing')
}
iterator = new Iterator(steps)
function iterateStep(target) {
const actualStep = iterator.actual()
, targetStep = iterator[target]()
if (targetStep.done) {
finish()
return
}
toggleStep(actualStep.value, targetStep.value)
targetStep.value.scrollIntoView()
}
next.forEach(b => b.addEventListener('click', e => {
e.preventDefault()
body.classList.add('ontour')
iterateStep('next')
}))
previous.forEach(b => b.addEventListener('click', e => {
e.preventDefault()
iterateStep('previous')
}))
finishBtn.forEach(b => b.addEventListener('click', finish))