-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
61 lines (46 loc) · 1.55 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
var nextBtn = document.querySelector('.next'),
prevBtn = document.querySelector('.prev'),
carousel = document.querySelector('.carousel'),
list = document.querySelector('.list'),
item = document.querySelectorAll('.item'),
runningTime = document.querySelector('.carousel .timeRunning')
let timeRunning = 3000
let timeAutoNext = 7000
nextBtn.onclick = function(){
showSlider('next')
}
prevBtn.onclick = function(){
showSlider('prev')
}
let runTimeOut
let runNextAuto = setTimeout(() => {
nextBtn.click()
}, timeAutoNext)
function resetTimeAnimation() {
runningTime.style.animation = 'none'
runningTime.offsetHeight /* trigger reflow */
runningTime.style.animation = null
runningTime.style.animation = 'runningTime 7s linear 1 forwards'
}
function showSlider(type) {
let sliderItemsDom = list.querySelectorAll('.carousel .list .item')
if(type === 'next'){
list.appendChild(sliderItemsDom[0])
carousel.classList.add('next')
} else{
list.prepend(sliderItemsDom[sliderItemsDom.length - 1])
carousel.classList.add('prev')
}
clearTimeout(runTimeOut)
runTimeOut = setTimeout( () => {
carousel.classList.remove('next')
carousel.classList.remove('prev')
}, timeRunning)
clearTimeout(runNextAuto)
runNextAuto = setTimeout(() => {
nextBtn.click()
}, timeAutoNext)
resetTimeAnimation() // Reset the running time animation
}
// Start the initial animation
resetTimeAnimation()