-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
98 lines (80 loc) · 2.92 KB
/
script.js
File metadata and controls
98 lines (80 loc) · 2.92 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
document.addEventListener('DOMContentLoaded', () => {
const sidebar = document.querySelector('.sidebar');
const hamburger = document.querySelector('.hamburger');
const navLinks = [...document.querySelectorAll('.nav-link')];
const sections = [...document.querySelectorAll('.content-section')];
hamburger?.addEventListener('click', () => sidebar.classList.toggle('open'));
navLinks.forEach(link => {
link.addEventListener('click', e => {
const href = link.getAttribute('href');
if (!href) return;
if (href.startsWith('#')) {
e.preventDefault();
const target = document.querySelector(href);
if (target) {
target.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
}
sidebar?.classList.remove('open');
});
});
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (!entry.isIntersecting) return;
navLinks.forEach(link => link.classList.remove('active'));
const activeLink = document.querySelector(`.nav-link[href="#${entry.target.id}"]`);
if (activeLink) activeLink.classList.add('active');
});
}, { root: null, rootMargin: '-25% 0px -60% 0px', threshold: 0 });
sections.forEach(section => observer.observe(section));
const slides = [...document.querySelectorAll('.slide')];
const prev = document.querySelector('[data-prev]');
const next = document.querySelector('[data-next]');
const slideImages = [...document.querySelectorAll('.slide img')];
const lightbox = document.createElement('div');
lightbox.className = 'lightbox';
lightbox.innerHTML = '<img alt="Expanded gallery image">';
document.body.appendChild(lightbox);
const lightboxImg = lightbox.querySelector('img');
let index = slides.findIndex(s => s.classList.contains('active'));
if (index < 0) index = 0;
let autoScroll;
const show = i => {
slides[index].classList.remove('active');
index = (i + slides.length) % slides.length;
slides[index].classList.add('active');
};
const startAutoScroll = () => {
stopAutoScroll();
autoScroll = setInterval(() => {
if (!lightbox.classList.contains('open')) show(index + 1);
}, 4500);
};
const stopAutoScroll = () => {
if (autoScroll) clearInterval(autoScroll);
};
prev?.addEventListener('click', () => {
show(index - 1);
startAutoScroll();
});
next?.addEventListener('click', () => {
show(index + 1);
startAutoScroll();
});
slideImages.forEach(image => {
image.addEventListener('click', () => {
const activeSlide = document.querySelector('.slide.active img');
if (activeSlide) {
lightboxImg.src = activeSlide.src;
lightboxImg.alt = activeSlide.alt;
lightbox.classList.add('open');
stopAutoScroll();
}
});
});
lightbox.addEventListener('click', () => {
lightbox.classList.remove('open');
startAutoScroll();
});
startAutoScroll();
});