-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
232 lines (199 loc) · 5.56 KB
/
app.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// hide header on scroll down and show on scroll up
(function () {
const doc = document.documentElement,
headerElement = document.querySelector('.header'),
headerElementHeight = parseInt(getComputedStyle(headerElement).height);
let currentScroll = null,
prevScroll = window.scrollY || doc.scrollTop,
direction = 0,
prevDirection = 0;
const checkRoll = () => {
currentScroll = window.scrollY || doc.scrollTop;
if (currentScroll > prevScroll) {
direction = 2;
} else if (currentScroll < prevScroll) {
direction = 1;
}
if (direction !== prevDirection) {
toggleHeader(direction, currentScroll);
}
prevScroll = currentScroll;
}
const toggleHeader = (direction, currentScroll) => {
if (direction === 2 && currentScroll > headerElementHeight) {
headerElement.classList.add('hide');
prevDirection = direction;
} else if (direction === 1) {
headerElement.classList.remove('hide');
prevDirection = direction
}
};
window.addEventListener('scroll', checkRoll)
}());
// smooth scroll to anchors
const anchors = document.querySelectorAll('.header__nav-link');
for (const anchor of anchors) {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const goTo = anchor.hasAttribute('href') ? anchor.getAttribute('href') : 'body';
document.querySelector(goTo).scrollIntoView({
behavior: 'smooth',
block: 'start'
})
})
}
// remove .why-item--right class on 962px screen width
const tabletWidth = 962,
whyItems = document.querySelectorAll('.why-item'),
rightClass = 'why-item--right'
for (const item of whyItems) {
if (item.classList.contains(rightClass) && screen.width <= tabletWidth) {
item.classList.remove(rightClass);
}
}
// sliders
const examplesSwiper = new Swiper('.examples__slider.swiper-container', {
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
})
const visualSwiper = new Swiper('.visual__slider.swiper-container', {
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
})
const statsSlider = new Swiper('.stats__slider.swiper-container', {
loop: true,
slidesPerView: 1,
breakpoints: {
1080: {
slidesPerView: 3
},
849: {
slidesPerView: 2
}
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
}
})
// form validation and sending
const constraints = {
name: {
presence: {
message: '^Пожалуйста, укажите имя'
}
},
phone: {
presence: {
message: '^Пожалуйста, укажите телефон'
},
format: {
pattern: /^(\+7|7|8)?[\s\-]?\(?[489][0-9]{2}\)?[\s\-]?[0-9]{3}[\s\-]?[0-9]{2}[\s\-]?[0-9]{2}$/,
flags: 'i',
message: '^Пожалуйста, укажите телефон в корректном формате'
}
},
email: {
presence: {
message: '^Пожалуйста, укажите email'
},
email: true
},
service: {
presence: {
message: '^Пожалуйста, выберите услугу'
},
},
agree: {
presence: {
message: '^Пожалуйста, отметьте этот пункт'
},
}
};
const knowMoreForm = document.forms['know-more-form'];
knowMoreForm.addEventListener('submit', function(e) {
e.preventDefault();
submitHandler(this);
})
const submitHandler = (form) => {
const values = validate.collectFormValues(form),
errors = validate(values, constraints);
showErrors(form, errors || {});
if (!errors) {
submitForm(form);
}
}
const showErrors = (form, errors) => {
const elements = [...form.elements];
elements.pop();
const errorElements = Object.keys(errors),
errorValues = Object.values(errors).flat().reverse();
elements.forEach(element => {
if (errorElements.includes(element.name)) {
element.classList.add('has-error');
} else if (!errorElements.includes(element.name)) {
element.classList.remove('has-error');
}
})
errorValues.forEach(errorVal => {
Toastify({
text: errorVal,
duration: 3500,
close: true,
gravity: 'top',
position: 'right',
backgroundColor: '#bd0000'
}).showToast()
})
}
const submitForm = (form) => {
const nameField = form.elements.name,
phoneField = form.elements.phone,
emailField = form.elements.email,
serviceField = form.elements.service,
agreeField = form.elements.agree,
submitButton = form.elements.submit;
submitButton.blur();
submitButton.disabled = true;
const formData = new FormData();
formData.append('name', nameField.value);
formData.append('phone', phoneField.value);
formData.append('email', emailField.value);
formData.append('service', serviceField.value);
formData.append('agree', agreeField.checked);
axios({
method: 'post',
url: '/mail.php',
data: formData,
headers: {
Accept: 'application/json',
'Content-Type': 'multipart/form-data',
},
})
.then(response => {
submitButton.disabled = false;
form.reset();
if (response.data.success) {
window.location.href = '/thanks.html';
} else {
const errors = Object.values(response.data.errors).reverse();
for (const error of errors) {
Toastify({
text: error,
duration: 3500,
close: true,
gravity: 'top',
position: 'right',
backgroundColor: '#bd0000'
}).showToast()
}
}
})
.catch(error => {
console.error(error)
});
}