-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
143 lines (118 loc) · 5.21 KB
/
script.js
File metadata and controls
143 lines (118 loc) · 5.21 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
// Smooth scrolling for navigation links
document.addEventListener('DOMContentLoaded', function() {
const navLinks = document.querySelectorAll('.nav-link');
navLinks.forEach(link => {
link.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href');
const targetSection = document.querySelector(targetId);
if (targetSection) {
const offsetTop = targetSection.offsetTop - 70; // Account for fixed navbar
window.scrollTo({
top: offsetTop,
behavior: 'smooth'
});
}
});
});
// Add active class to navigation links based on scroll position
window.addEventListener('scroll', function() {
const sections = document.querySelectorAll('section[id]');
const scrollPos = window.scrollY + 100;
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.offsetHeight;
const sectionId = section.getAttribute('id');
if (scrollPos >= sectionTop && scrollPos < sectionTop + sectionHeight) {
// Remove active class from all links
navLinks.forEach(link => link.classList.remove('active'));
// Add active class to current section's link
const activeLink = document.querySelector(`.nav-link[href="#${sectionId}"]`);
if (activeLink) {
activeLink.classList.add('active');
}
}
});
});
// Add fade-in animation for elements as they come into view
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver(function(entries) {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.style.opacity = '1';
entry.target.style.transform = 'translateY(0)';
}
});
}, observerOptions);
// Observe all cards and sections for animation
const animatedElements = document.querySelectorAll('.analysis-card, .insight-category, .stat-card, .mission, .methodology, .impact');
animatedElements.forEach(el => {
el.style.opacity = '0';
el.style.transform = 'translateY(30px)';
el.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
observer.observe(el);
});
// Initialize all carousels
function initializeCarousel(carousel) {
const track = carousel.querySelector('.carousel-track');
const slides = Array.from(carousel.querySelectorAll('.carousel-slide'));
const btnPrev = carousel.querySelector('.carousel-btn.prev');
const btnNext = carousel.querySelector('.carousel-btn.next');
const captionIndex = carousel.querySelector('.carousel-index');
const captionText = carousel.querySelector('.carousel-text');
const dotsContainer = carousel.querySelector('.carousel-dots');
if (!track || !slides.length || !btnPrev || !btnNext) return;
let current = 0;
// Build dots
slides.forEach((_, idx) => {
const dot = document.createElement('button');
dot.className = 'carousel-dot';
dot.setAttribute('aria-label', `Go to slide ${idx + 1}`);
dot.addEventListener('click', () => goTo(idx));
dotsContainer.appendChild(dot);
});
function update() {
const offset = -current * 100;
track.style.transform = `translateX(${offset}%)`;
if (captionIndex) captionIndex.textContent = `${current + 1} / ${slides.length}`;
if (captionText) captionText.textContent = slides[current].dataset.caption || '';
Array.from(dotsContainer.children).forEach((d, i) => {
d.classList.toggle('active', i === current);
});
}
function goTo(idx) {
current = (idx + slides.length) % slides.length;
update();
}
btnPrev.addEventListener('click', (e) => {
e.preventDefault();
goTo(current - 1);
});
btnNext.addEventListener('click', (e) => {
e.preventDefault();
goTo(current + 1);
});
// Keyboard navigation
carousel.addEventListener('keydown', (e) => {
if (e.key === 'ArrowLeft') goTo(current - 1);
if (e.key === 'ArrowRight') goTo(current + 1);
});
// Swipe support (basic)
let startX = 0;
track.addEventListener('touchstart', (e) => { startX = e.touches[0].clientX; });
track.addEventListener('touchend', (e) => {
const dx = e.changedTouches[0].clientX - startX;
if (dx > 40) goTo(current - 1);
if (dx < -40) goTo(current + 1);
});
// Initialize
update();
carousel.setAttribute('tabindex', '0');
}
// Initialize all carousels
const allCarousels = document.querySelectorAll('.map-carousel');
allCarousels.forEach(initializeCarousel);
});