-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
119 lines (100 loc) · 3.19 KB
/
main.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
106
107
108
109
110
111
112
113
114
115
116
117
class Timer {
static optionsDefault = {
duration: 60,
step: 1000,
selectors: {
timer: '.timer',
message: '.timer-message',
button: '.timer-button',
}
}
constructor(optionsCustom) {
this.timer = false
this.isStopped = true
this.options = {
...Timer.optionsDefault,
...optionsCustom
}
this.selectors = {
...Timer.optionsDefault.selectors,
...optionsCustom.selectors,
}
this.remainingTime = this.options.duration
this.elements = this.updateElements()
this.onClick = this.handleClick.bind(this)
}
updateElements() {
const { body } = document
const timerElement = body.querySelector(this.selectors.timer)
const timerMessage = timerElement.querySelector(this.selectors.message)
const timerButton = timerElement.querySelector(this.selectors.button)
return {
timerElement, timerMessage, timerButton
}
}
declOfNum(number, titles) {
const cases = [2, 0, 1, 1, 1, 2]
return titles[number % 100 > 4 && number % 100 < 20 ? 2 : cases[number % 10 < 5 ? number % 10 : 5]]
}
renderTime() {
this.remainingTime -= 1
this.elements.timerMessage.textContent = 'Повторная отправка через ' + this.remainingTime + ' ' + this.declOfNum(this.remainingTime, ['секунду', 'секунды', 'секунд'])
if (this.remainingTime === 0) this.setDefaultState()
}
init() {
this.elements = this.updateElements()
this.setDefaultState()
this.updateListeners()
}
updateListeners() {
document.removeEventListener('click', this.onClick)
document.addEventListener('click', this.onClick)
}
handleClick(event) {
const { target } = event
if (target.closest(this.selectors.button)) this.startTimer()
}
setDefaultState() {
this.isStopped = true
this.remainingTime = this.options.duration
this.elements.timerMessage.textContent = 'Можно отправить повторно'
this.elements.timerButton.classList.remove('disabled')
clearInterval(this.timer)
}
startTimer() {
if (this.isStopped) {
this.isStopped = false
this.elements.timerMessage.textContent = `До повторной отправки ${ this.options.duration } секунд`
this.elements.timerButton.classList.add('disabled')
this.timer = setInterval(this.renderTime.bind(this), this.options.step)
}
}
}
document.addEventListener('DOMContentLoaded', () => {
const buttonTimer = new Timer({
duration: 10,
})
buttonTimer.init()
const loaderThird = document.body.querySelector('.loader-third')
const elements = [
...loaderThird.querySelectorAll('path'),
...loaderThird.querySelectorAll('circle')
]
elements.forEach((element) => element.style.transformOrigin = 'center')
function update(duration) {
elements.forEach((element, index) => {
element.animate({
// opacity: [Math.random(), 1],
transform: [
'scale(0.75) rotate(0deg)',
`scale(1) rotate(${ 180 * index }deg)`,
'scale(0.75) rotate(0deg)',
],
},
duration,
)
})
setTimeout(() => requestAnimationFrame(update), duration)
}
update(1000)
})