Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
410 lines (367 loc) · 13.2 KB
/
Copy pathscript.js
File metadata and controls
410 lines (367 loc) · 13.2 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
(function () {
'use strict';
/* Prevent the browser's native hash autoscroll on load so our
programmatic scroll (which accounts for the fixed navbar + section
gap) is the single source of truth. Without this, a URL like
index.html#services gets a native jump that fights Lenis. */
if ('scrollRestoration' in history) {
history.scrollRestoration = 'manual';
}
/* ── Lenis smooth scroll (graceful fallback if CDN fails) ── */
var lenis = null;
if (typeof Lenis !== 'undefined') {
lenis = new Lenis({
duration: 1.2,
easing: function (t) { return Math.min(1, 1.001 - Math.pow(2, -10 * t)); },
orientation: 'vertical',
smoothWheel: true,
});
function raf(time) {
lenis.raf(time);
requestAnimationFrame(raf);
}
requestAnimationFrame(raf);
}
// Null-safe scroll helpers: work with or without Lenis
function scrollTo(target, opts) {
opts = opts || {};
var offset = opts.offset || 0;
var top;
if (typeof target === 'number') {
top = target;
} else if (target && target.getBoundingClientRect) {
top = target.getBoundingClientRect().top + (window.scrollY || window.pageYOffset || 0);
} else {
return;
}
top += offset;
if (lenis) {
lenis.scrollTo(top, { duration: opts.duration || 1.2, easing: opts.easing });
} else {
window.scrollTo({ top: top, behavior: 'smooth' });
}
}
function onScroll(cb) {
if (lenis) {
lenis.on('scroll', cb);
} else {
window.addEventListener('scroll', function () {
cb({ animatedScroll: window.scrollY });
}, { passive: true });
}
}
/* ── Theme switcher ── */
function setTheme(theme) {
var root = document.documentElement;
root.classList.remove('dark', 'light', 'bright');
root.classList.add(theme);
try { localStorage.setItem('arental_theme', theme); } catch (e) {}
document.querySelectorAll('.theme-btn').forEach(function (btn) {
btn.classList.toggle('active', btn.getAttribute('data-theme') === theme);
});
var meta = document.querySelector('meta[name="theme-color"]');
if (meta) {
var bg = getComputedStyle(root).getPropertyValue('--bg').trim() || '#0a0a0c';
meta.setAttribute('content', bg);
}
}
document.querySelectorAll('.theme-btn').forEach(function (btn) {
btn.addEventListener('click', function () {
setTheme(btn.getAttribute('data-theme'));
});
});
var savedTheme;
try { savedTheme = localStorage.getItem('arental_theme'); } catch (e) {}
setTheme(savedTheme || 'dark');
/* ── Navbar hide/show on scroll ── */
var navbar = document.getElementById('navbar');
var lastScroll = 0;
var anchoring = false;
if (navbar) {
onScroll(function (e) {
if (anchoring) {
navbar.classList.remove('hidden');
return;
}
var currentScroll = e.animatedScroll;
if (currentScroll > 80 && currentScroll > lastScroll) {
navbar.classList.add('hidden');
} else {
navbar.classList.remove('hidden');
}
lastScroll = currentScroll;
});
}
/* ── Mobile menu ── */
var burger = document.getElementById('navBurger');
var overlay = document.getElementById('menuOverlay');
if (burger && overlay) {
function closeMenu() {
overlay.classList.remove('open');
overlay.setAttribute('inert', '');
burger.classList.remove('active');
burger.setAttribute('aria-expanded', 'false');
document.body.style.overflow = '';
burger.focus();
}
function openMenu() {
overlay.classList.add('open');
overlay.removeAttribute('inert');
burger.classList.add('active');
burger.setAttribute('aria-expanded', 'true');
document.body.style.overflow = 'hidden';
var firstLink = overlay.querySelector('a');
if (firstLink) firstLink.focus();
}
function toggleMenu() {
if (overlay.classList.contains('open')) {
closeMenu();
} else {
openMenu();
}
}
burger.setAttribute('aria-expanded', 'false');
burger.setAttribute('aria-controls', 'menuOverlay');
burger.addEventListener('click', toggleMenu);
overlay.querySelectorAll('a').forEach(function (link) {
link.addEventListener('click', closeMenu);
});
overlay.addEventListener('click', function (e) {
if (e.target === overlay) closeMenu();
});
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && overlay.classList.contains('open')) {
closeMenu();
}
});
}
/* ── Review slider ── */
var reviewIdx = 0;
var track = document.getElementById('reviewsTrack');
var dots = document.getElementById('reviewsDots');
var prevBtn = document.getElementById('prevReview');
var nextBtn = document.getElementById('nextReview');
function updateReviews() {
if (!track || !track.children.length) return;
track.style.transform = 'translateX(' + (-reviewIdx * 100) + '%)';
if (dots) {
var dotEls = dots.querySelectorAll('.dot');
dotEls.forEach(function (d, i) {
d.classList.toggle('active', i === reviewIdx);
});
}
}
if (nextBtn) {
nextBtn.addEventListener('click', function () {
reviewIdx = (reviewIdx + 1) % track.children.length;
updateReviews();
});
}
if (prevBtn) {
prevBtn.addEventListener('click', function () {
if (!track || !track.children.length) return;
reviewIdx = (reviewIdx - 1 + track.children.length) % track.children.length;
updateReviews();
});
}
if (dots) {
dots.querySelectorAll('.dot').forEach(function (dot, i) {
dot.setAttribute('role', 'button');
dot.setAttribute('tabindex', '0');
dot.setAttribute('aria-label', 'Отзыв ' + (i + 1));
dot.addEventListener('click', function () {
reviewIdx = i;
updateReviews();
});
dot.addEventListener('keydown', function (e) {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
reviewIdx = i;
updateReviews();
}
});
});
}
setInterval(function () {
if (!track || !track.children.length) return;
reviewIdx = (reviewIdx + 1) % track.children.length;
updateReviews();
}, 5000);
/* ── FAQ accordion ── */
var faqItems = document.querySelectorAll('.faq-item');
faqItems.forEach(function (item) {
var q = item.querySelector('.faq-q');
if (q) {
q.addEventListener('click', function () {
faqItems.forEach(function (other) {
if (other !== item) other.classList.remove('open');
});
item.classList.toggle('open');
});
}
});
/* ── Scroll reveal ── */
var revealObserver = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
revealObserver.unobserve(entry.target);
}
});
}, { threshold: 0.1, rootMargin: '0px 0px -40px 0px' });
document.querySelectorAll('.reveal').forEach(function (el) {
revealObserver.observe(el);
});
/* ── Animated counters ── */
var counterObserver = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
var el = entry.target;
var target = parseInt(el.getAttribute('data-target'), 10);
var prefix = el.getAttribute('data-prefix') || '';
var suffix = el.getAttribute('data-suffix') || '';
if (isNaN(target)) return;
var duration = 1500;
var startTime = null;
function step(timestamp) {
if (!startTime) startTime = timestamp;
var progress = Math.min((timestamp - startTime) / duration, 1);
var eased = 1 - Math.pow(1 - progress, 3);
el.textContent = prefix + Math.floor(eased * target) + suffix;
if (progress < 1) {
requestAnimationFrame(step);
} else {
el.textContent = prefix + target + suffix;
}
}
requestAnimationFrame(step);
counterObserver.unobserve(el);
}
});
}, { threshold: 0.5 });
document.querySelectorAll('.counter').forEach(function (el) {
counterObserver.observe(el);
});
/* ── Logo scroll to top ── */
var logoTop = document.getElementById('logoTop');
if (logoTop) {
logoTop.addEventListener('click', function () {
scrollTo(0, { duration: 1.2 });
});
logoTop.addEventListener('keydown', function (e) {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
scrollTo(0, { duration: 1.2 });
}
});
}
/* ── Back to top ── */
var backToTop = document.getElementById('backToTop');
if (backToTop) {
onScroll(function (e) {
if (e.animatedScroll > 500) {
backToTop.classList.add('visible');
backToTop.removeAttribute('inert');
} else {
backToTop.classList.remove('visible');
backToTop.setAttribute('inert', '');
}
});
backToTop.addEventListener('click', function () {
scrollTo(0, { duration: 1.2 });
});
}
/* ── Contact form mailto ── */
var contactForm = document.querySelector('.contact-form');
if (contactForm) {
contactForm.addEventListener('submit', function (e) {
e.preventDefault();
var name = this.querySelector('input[type="text"]').value.trim();
var phone = this.querySelector('input[type="tel"]').value.trim();
var email = this.querySelector('input[type="email"]').value.trim();
var message = this.querySelector('textarea').value.trim();
var subject = encodeURIComponent('Заявка с сайта АТМОСФЕРА от ' + name);
var body = encodeURIComponent(
'Имя: ' + name + '\n' +
'Телефон: ' + phone + '\n' +
'Email: ' + email + '\n' +
'Сообщение: ' + message
);
window.location.href = 'mailto:hello@arental.ru?subject=' + subject + '&body=' + body;
alert('Спасибо! Мы получили вашу заявку и свяжемся с вами.');
});
}
/* ── Smooth anchor links ── */
document.querySelectorAll('a[href^="#"]').forEach(function (a) {
a.addEventListener('click', function (e) {
var href = this.getAttribute('href');
if (!href || href.length <= 1) return;
var target = document.getElementById(href.slice(1));
if (!target) return;
e.preventDefault();
var navH = navbar ? navbar.offsetHeight : 0;
var reveals = target.querySelectorAll('.reveal');
var wasVisible = [];
reveals.forEach(function (r, i) {
wasVisible[i] = r.classList.contains('visible');
r.style.transition = 'none';
r.classList.add('visible');
});
var heading = target.querySelector('h2, h1, h3');
var gap = heading ? (heading.getBoundingClientRect().top - target.getBoundingClientRect().top) : 0;
reveals.forEach(function (r, i) {
r.style.transition = '';
if (!wasVisible[i]) r.classList.remove('visible');
});
anchoring = true;
scrollTo(target, { duration: 1.2, offset: gap - navH - 8 });
setTimeout(function () { anchoring = false; }, 1400);
if (history.pushState) {
history.pushState(null, '', href);
} else {
location.hash = href;
}
});
});
/* ── Honour initial URL hash on load ── */
/* The inline <head> script strips the hash from the URL before the browser
could perform its native fragment jump, then exposes it as
window.__initialHash. We scroll to it here with a navbar-aware offset. */
var initialHash = window.__initialHash || '';
if (initialHash && initialHash.length > 1) {
var initialTarget = document.getElementById(initialHash.slice(1));
if (initialTarget) {
var navH = navbar ? navbar.offsetHeight : 0;
var reveals = initialTarget.querySelectorAll('.reveal');
var wasVisible = [];
reveals.forEach(function (r, i) {
wasVisible[i] = r.classList.contains('visible');
r.style.transition = 'none';
r.classList.add('visible');
});
var heading = initialTarget.querySelector('h2, h1, h3');
var gap = heading ? (heading.getBoundingClientRect().top - initialTarget.getBoundingClientRect().top) : 0;
reveals.forEach(function (r, i) {
r.style.transition = '';
if (!wasVisible[i]) r.classList.remove('visible');
});
function runInitialScroll() {
anchoring = true;
scrollTo(initialTarget, { duration: 1.0, offset: gap - navH - 8 });
/* Restore the hash in the address bar (replaceState never scrolls). */
try { history.replaceState(null, '', initialHash); } catch (e) {}
setTimeout(function () { anchoring = false; }, 1200);
}
/* Run after the browser's native fragment jump so ours wins. */
if (document.readyState === 'complete') {
setTimeout(runInitialScroll, 60);
} else {
window.addEventListener('load', function () {
setTimeout(runInitialScroll, 60);
});
/* Fallback re-assert in case the native jump lands even later. */
setTimeout(runInitialScroll, 900);
}
}
}
})();