-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
171 lines (149 loc) · 5.42 KB
/
script.js
File metadata and controls
171 lines (149 loc) · 5.42 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
// Setup for scroll-triggered animations
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
// Watch sections and trigger animations when they come into view
const animateOnScroll = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate-in');
// Animate bar chart when it appears
if (entry.target.classList.contains('bar-chart')) {
animateBarChart();
}
}
});
}, observerOptions);
// Start observing sections when page loads
document.addEventListener('DOMContentLoaded', () => {
const sections = document.querySelectorAll('section');
const charts = document.querySelectorAll('.bar-chart');
sections.forEach(section => {
animateOnScroll.observe(section);
});
charts.forEach(chart => {
animateOnScroll.observe(chart);
});
// Add fade-in effect for sections
const style = document.createElement('style');
style.textContent = `
section {
opacity: 0;
transform: translateY(30px);
transition: opacity 0.8s ease, transform 0.8s ease;
}
section.animate-in {
opacity: 1;
transform: translateY(0);
}
.hero-section {
opacity: 1;
transform: none;
}
`;
document.head.appendChild(style);
});
// Animate bars growing to their heights
function animateBarChart() {
const bars = document.querySelectorAll('.bar');
bars.forEach((bar, index) => {
setTimeout(() => {
const value = parseFloat(bar.dataset.value);
const maxHeight = 250;
const maxValue = 50;
const height = (value / maxValue) * maxHeight;
bar.style.height = `${height}px`;
}, index * 250);
});
}
// Smooth scrolling for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
// Share button functionality
document.addEventListener('DOMContentLoaded', () => {
const shareBtn = document.querySelector('.cta-section .cta-button');
if (shareBtn) {
shareBtn.addEventListener('click', () => {
// Try native share API first
if (navigator.share) {
navigator.share({
title: 'Move Toronto Public Transit Forward',
text: 'Toronto\'s transit system needs improvement. Learn how better transit can help our city grow.',
url: window.location.href
}).catch(err => console.log('Error sharing:', err));
} else {
// Fallback to copying URL
navigator.clipboard.writeText(window.location.href).then(() => {
const originalText = shareBtn.textContent;
shareBtn.textContent = 'Link Copied!';
setTimeout(() => {
shareBtn.textContent = originalText;
}, 2000);
}).catch(() => {
alert('Share this page: ' + window.location.href);
});
}
});
}
});
// Subtle parallax effect for hero background
function throttle(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
const throttledScroll = throttle(() => {
const scrolled = window.pageYOffset;
const heroBackground = document.querySelector('.hero-background-animation');
if (heroBackground && scrolled < window.innerHeight) {
const speed = scrolled * 0.5;
heroBackground.style.transform = `translate(${speed * 0.1}px, ${speed * 0.1}px)`;
}
}, 16);
window.addEventListener('scroll', throttledScroll);
// Trigger initial animations
window.addEventListener('load', () => {
document.body.classList.add('loaded');
const heroSection = document.querySelector('.hero-section');
if (heroSection) {
heroSection.classList.add('animate-in');
}
});
// Respect user's motion preferences
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)');
if (prefersReducedMotion.matches) {
const style = document.createElement('style');
style.textContent = `
*, *::before, *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
`;
document.head.appendChild(style);
}
// Log errors for debugging
window.addEventListener('error', (e) => {
console.log('An error occurred:', e.error);
});
// Fun console messages
console.log('%cMove Toronto Forward 🚇', 'color: #BC5135; font-size: 20px; font-weight: bold;');
console.log('%cThis website supports UN SDG 9: Industry, Innovation & Infrastructure', 'color: #84BCB6; font-size: 14px;');
console.log('%cDesigned by Hui Ke, OCAD University', 'color: #666; font-size: 12px;');