-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
140 lines (118 loc) · 6.45 KB
/
script.js
File metadata and controls
140 lines (118 loc) · 6.45 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
// Navigation and scroll functionality
document.addEventListener('DOMContentLoaded', function() {
const sections = document.querySelectorAll('.content-section');
const sidebarContents = document.querySelectorAll('.sidebar-content');
const navLinks = document.querySelectorAll('.nav-link');
const rightSidebar = document.querySelector('.right-sidebar');
const mainContent = document.querySelector('.main-content');
let currentSectionIndex = 0;
// Function to update active states
function updateActiveStates() {
const scrollPosition = window.scrollY + 200; // Offset for header
sections.forEach((section, index) => {
const sectionTop = section.offsetTop;
const sectionHeight = section.offsetHeight;
const sectionId = section.getAttribute('id');
// Check if section is in view
if (scrollPosition >= sectionTop && scrollPosition < sectionTop + sectionHeight) {
const previousIndex = currentSectionIndex;
currentSectionIndex = index;
// All sections behave the same - no special handling needed
// Update sidebar content
sidebarContents.forEach(content => {
content.classList.remove('active');
});
// Special handling for subnational inequalities section (index 3)
if (index === 3) {
// Find which figure is currently in view
const figures = section.querySelectorAll('figure[data-graph-type]');
let activeGraphType = null;
const viewportCenter = window.innerHeight / 2 + window.scrollY;
figures.forEach(figure => {
const rect = figure.getBoundingClientRect();
const figureCenter = rect.top + rect.height / 2 + window.scrollY;
// Check if figure center is in viewport center area
if (Math.abs(figureCenter - viewportCenter) < window.innerHeight / 3) {
activeGraphType = figure.getAttribute('data-graph-type');
}
});
// Fallback: check which figure is most in view
if (!activeGraphType && figures.length > 0) {
let closestFigure = null;
let closestDistance = Infinity;
figures.forEach(figure => {
const rect = figure.getBoundingClientRect();
const figureTop = rect.top + window.scrollY;
const figureBottom = figureTop + rect.height;
const viewportTop = window.scrollY;
const viewportBottom = window.scrollY + window.innerHeight;
// Check how much of figure is visible
const visibleTop = Math.max(figureTop, viewportTop);
const visibleBottom = Math.min(figureBottom, viewportBottom);
const visibleHeight = Math.max(0, visibleBottom - visibleTop);
if (visibleHeight > rect.height * 0.3) { // At least 30% visible
const centerDistance = Math.abs((figureTop + figureBottom) / 2 - (viewportTop + viewportBottom) / 2);
if (centerDistance < closestDistance) {
closestDistance = centerDistance;
closestFigure = figure;
}
}
});
if (closestFigure) {
activeGraphType = closestFigure.getAttribute('data-graph-type');
} else {
// Default to first figure type
activeGraphType = figures[0].getAttribute('data-graph-type');
}
}
// Show corresponding sidebar content
if (activeGraphType === 'health') {
const healthContent = document.querySelector('[data-content="3-health"]');
if (healthContent) {
healthContent.classList.add('active');
}
} else if (activeGraphType === 'education') {
const educationContent = document.querySelector('[data-content="3-education"]');
if (educationContent) {
educationContent.classList.add('active');
}
}
} else {
// Regular section handling
const correspondingContent = document.querySelector(`[data-content="${index}"]`);
if (correspondingContent) {
correspondingContent.classList.add('active');
}
}
// Update nav links
navLinks.forEach(link => {
link.classList.remove('active');
});
const correspondingLink = document.querySelector(`a[href="#${sectionId}"]`);
if (correspondingLink) {
correspondingLink.classList.add('active');
}
}
});
}
// Update on scroll
window.addEventListener('scroll', updateActiveStates);
// Update on initial load
updateActiveStates();
// Smooth scroll for navigation links
navLinks.forEach(link => {
link.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href');
const targetSection = document.querySelector(targetId);
if (targetSection) {
const headerHeight = document.querySelector('.main-header').offsetHeight;
const targetPosition = targetSection.offsetTop - headerHeight;
window.scrollTo({
top: targetPosition,
behavior: 'smooth'
});
}
});
});
});