-
Notifications
You must be signed in to change notification settings - Fork 0
/
testimonials-slider.js
105 lines (74 loc) · 3.08 KB
/
testimonials-slider.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
let cases = []
let currentCaseIndex = null
let controls = []
let testimonialsInterval = null
const activateCase = (caseElement) => {
let caseIndex = cases.indexOf(caseElement)
controls[caseIndex].classList.remove('cursor-pointer')
controls[caseIndex].classList.add('active', '!w-6', '!bg-greyscale-400')
caseElement.classList.remove('hidden')
}
const deactivateCase = (caseElement) => {
let caseIndex = cases.indexOf(caseElement)
if ( !controls[caseIndex].classList.contains('cursor-pointer') ) controls[caseIndex].classList.add('cursor-pointer')
controls[caseIndex].classList.remove('active', '!w-6', '!bg-greyscale-400')
if ( !caseElement.classList.contains('hidden') ) caseElement.classList.add('hidden')
}
const caseProcessor = (caseElement, caseIndex) => {
caseIndex === currentCaseIndex ? activateCase(caseElement) : deactivateCase(caseElement)
}
const startTestimonialsInterval = () => {
if ( currentCaseIndex === null ) currentCaseIndex = 0
else currentCaseIndex = (currentCaseIndex + 1) % cases.length
cases.forEach(caseProcessor)
}
const insertControls = (controlsArea) => {
cases.forEach(() => {
let control = document.createElement('li')
control.classList.add('cursor-pointer', 'h-2', 'w-2', 'rounded-full', 'bg-greyscale-400/25', 'hover:bg-greyscale-400/75')
controlsArea.appendChild(control)
controls.push(control)
})
}
const listenTestimonialsEvents = (cases) => {
controls.forEach(listenControlClickForActivate)
cases.forEach(listenCaseMouseOverForResetInterval)
cases.forEach(listenCaseMouseOutForStartInterval)
}
const listenControlClickForActivate = controlElement => {
controlElement.addEventListener('click', event => {
event.preventDefault()
event.stopPropagation()
if ( controlElement.classList.contains('active') ) return;
clearInterval(testimonialsInterval)
testimonialsInterval = null
currentCaseIndex = controls.indexOf(controlElement) - 1
if ( testimonialsInterval === null ) testimonialsInterval = setInterval(startTestimonialsInterval, 5000)
startTestimonialsInterval()
})
}
const listenCaseMouseOverForResetInterval = caseElement => {
caseElement.addEventListener('mouseover', event => {
event.stopPropagation()
if ( caseElement.classList.contains('hidden') ) return;
clearInterval(testimonialsInterval)
testimonialsInterval = null
})
}
const listenCaseMouseOutForStartInterval = caseElement => {
caseElement.addEventListener('mouseout', event => {
event.stopPropagation()
if ( caseElement.classList.contains('hidden') ) return;
if ( testimonialsInterval === null ) testimonialsInterval = setInterval(startTestimonialsInterval, 5000)
})
}
(function() {
const slider = document.getElementById('testimonials')
if ( !slider ) return;
let controlsArea = slider.querySelector('#controls')
cases = Array.from(slider.querySelectorAll('#cases > li'))
insertControls(controlsArea)
testimonialsInterval = setInterval(startTestimonialsInterval, 6000)
startTestimonialsInterval()
listenTestimonialsEvents(cases)
})();