-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
346 lines (294 loc) · 10.8 KB
/
script.js
File metadata and controls
346 lines (294 loc) · 10.8 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
// ====== LINK TO SURVEY ======
const SURVEY_URL = "https://docs.google.com/forms/d/e/1FAIpQLSeSzNoEabj9xiWyiiBPO7JtZOCQKI-l8xrHOQ-q1bFFE77wDg/viewform?usp=sharing&ouid=103231803179167534745";
// ============================
const toast = document.getElementById("toast");
const showToast = (msg="Done!") => {
toast.textContent = msg;
toast.classList.add("show");
setTimeout(()=>toast.classList.remove("show"), 1400);
};
// Survey buttons
const surveyBtn = document.getElementById("surveyBtn");
if(surveyBtn) {
surveyBtn.href = SURVEY_URL;
surveyBtn.target = "_blank";
}
const copyLinkBtn = document.getElementById("copyLinkBtn");
if(copyLinkBtn){
copyLinkBtn.addEventListener("click", async () => {
if (!SURVEY_URL) return showToast("No survey link yet.");
await navigator.clipboard.writeText(SURVEY_URL);
showToast("Survey link copied!");
});
}
// Local/Global toggle
const scopeSwitch = document.getElementById("scopeSwitch");
const scopeLabel = document.getElementById("scopeLabel");
const factTitle = document.querySelector("#factCard .mini-title");
const factText = document.getElementById("factText");
const localFact = {
title: "Local focus (Nice)",
text: "Nice faces increasing drought pressure and high summer demand. Let’s reduce waste at school and at home."
};
const globalFact = {
title: "Global context",
text: "Many regions face water scarcity. Saving water locally helps reduce pressure and builds sustainable habits."
};
const setScope = (isGlobal) => {
if (isGlobal) {
scopeSwitch.classList.add("on");
scopeSwitch.setAttribute("aria-checked", "true");
scopeLabel.textContent = "Show local focus";
factTitle.textContent = globalFact.title;
factText.textContent = globalFact.text;
} else {
scopeSwitch.classList.remove("on");
scopeSwitch.setAttribute("aria-checked", "false");
scopeLabel.textContent = "Show global context";
factTitle.textContent = localFact.title;
factText.textContent = localFact.text;
}
localStorage.setItem("scopeGlobal", JSON.stringify(isGlobal));
};
const savedScope = JSON.parse(localStorage.getItem("scopeGlobal") || "false");
if(scopeSwitch) setScope(savedScope);
if(scopeSwitch){
scopeSwitch.addEventListener("click", ()=> setScope(!scopeSwitch.classList.contains("on")));
scopeSwitch.addEventListener("keydown", (e)=>{
if (e.key === "Enter" || e.key === " ") { e.preventDefault(); setScope(!scopeSwitch.classList.contains("on")); }
});
}
// Tips
const tips = [
"Turn off the tap while brushing.",
"Report leaking taps at school.",
"Choose shorter showers this week.",
"Refill your bottle instead of buying new ones.",
"Only run full loads in the dishwasher/washing machine.",
"Close taps fully—no drips.",
"Encourage your family to try the 7-day challenge."
];
const tipEl = document.getElementById("tipOfDay");
const newTipBtn = document.getElementById("newTipBtn");
if(newTipBtn){
newTipBtn.addEventListener("click", ()=>{
const pick = tips[Math.floor(Math.random()*tips.length)];
tipEl.textContent = pick;
showToast("New tip!");
});
}
// 7-day challenge checklist
const checklist = document.getElementById("checklist");
const progressText = document.getElementById("progressText");
const progressPercent = document.getElementById("progressPercent");
const progressBar = document.getElementById("progressBar");
const KEY = "teamwater_challenge";
const loadChecks = () => JSON.parse(localStorage.getItem(KEY) || "[false,false,false,false,false,false,false]");
const saveChecks = (arr) => localStorage.setItem(KEY, JSON.stringify(arr));
const renderChecklist = () => {
if(!checklist) return;
const checks = loadChecks();
checklist.innerHTML = "";
checks.forEach((val, idx) => {
const id = `day${idx+1}`;
const label = document.createElement("label");
label.className = "chk";
label.innerHTML = `
<input type="checkbox" id="${id}" ${val ? "checked" : ""} />
<div>
<strong>Day ${idx+1}</strong>
<div class="muted">Complete one water-saving action today.</div>
</div>
`;
checklist.appendChild(label);
const cb = label.querySelector("input");
cb.addEventListener("change", ()=>{
const updated = loadChecks();
updated[idx] = cb.checked;
saveChecks(updated);
updateProgress();
if (updated.every(Boolean)) celebrate();
});
});
updateProgress();
};
const updateProgress = () => {
if(!progressText) return;
const checks = loadChecks();
const done = checks.filter(Boolean).length;
progressText.textContent = done;
const pct = Math.round((done/7)*100);
progressPercent.textContent = pct + "%";
progressBar.style.width = pct + "%";
};
const resetBtn = document.getElementById("resetChallengeBtn");
if(resetBtn){
resetBtn.addEventListener("click", ()=>{
saveChecks([false,false,false,false,false,false,false]);
renderChecklist();
showToast("Reset!");
});
}
const shareBtn = document.getElementById("shareProgressBtn");
if(shareBtn){
shareBtn.addEventListener("click", async ()=>{
const done = loadChecks().filter(Boolean).length;
const text = `#DropByDrop challenge: I completed ${done}/7 days!`;
await navigator.clipboard.writeText(text);
showToast("Progress text copied!");
});
}
const celebrate = () => {
showToast("Challenge completed! 🎉");
for (let i=0;i<28;i++){
const d = document.createElement("div");
d.className = "dot";
const x = Math.random()*window.innerWidth;
const y = window.innerHeight - 80;
const dx = (Math.random()*2 - 1) * 220 + "px";
const dy = -(Math.random()*260 + 120) + "px";
d.style.left = x + "px";
d.style.top = y + "px";
d.style.setProperty("--dx", dx);
d.style.setProperty("--dy", dy);
d.style.background = `rgba(${Math.floor(120+Math.random()*135)},${Math.floor(120+Math.random()*135)},255,.9)`;
document.body.appendChild(d);
setTimeout(()=>d.remove(), 1000);
}
};
renderChecklist();
// Note: Removed "Update Stats" logic as requested.
// To change stats, edit the HTML directly.
// Pledge wall
const PLEDGE_KEY = "teamwater_pledges";
const pledgeWall = document.getElementById("pledgeWall");
const pledgeInput = document.getElementById("pledgeInput");
const addPledgeBtn = document.getElementById("addPledgeBtn");
const clearPledgesBtn = document.getElementById("clearPledgesBtn");
const loadPledges = () => JSON.parse(localStorage.getItem(PLEDGE_KEY) || "[]");
const savePledges = (arr) => localStorage.setItem(PLEDGE_KEY, JSON.stringify(arr));
const renderPledges = () => {
if(!pledgeWall) return;
const pledges = loadPledges();
pledgeWall.innerHTML = pledges.length ? "" : `<div class="muted">No pledges yet!</div>`;
pledges.slice().reverse().forEach(p => {
const div = document.createElement("div");
div.className = "pledge";
div.innerHTML = `<span>${escapeHtml(p.text)}</span><small>${new Date(p.time).toLocaleDateString()}</small>`;
pledgeWall.appendChild(div);
});
};
const escapeHtml = (str) => str.replace(/[&<>"']/g, m => ({
"&":"&","<":"<",">":">",'"':""","'":"'"
}[m]));
if(addPledgeBtn){
addPledgeBtn.addEventListener("click", ()=>{
const text = pledgeInput.value.trim();
if (!text) return showToast("Write a pledge first.");
const pledges = loadPledges();
pledges.push({ text, time: Date.now() });
savePledges(pledges);
pledgeInput.value = "";
renderPledges();
showToast("Pledge added!");
});
}
if(clearPledgesBtn){
clearPledgesBtn.addEventListener("click", ()=>{
savePledges([]);
renderPledges();
showToast("Cleared.");
});
}
renderPledges();
// Smooth scroll helper
function smoothScrollTo(targetY, duration = 1600) {
const startY = window.scrollY;
const diff = targetY - startY;
const startTime = performance.now();
const easeInOutCubic = (t) =>
t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
function step(now) {
const elapsed = now - startTime;
const t = Math.min(1, elapsed / duration);
const eased = easeInOutCubic(t);
window.scrollTo(0, startY + diff * eased);
if (t < 1) requestAnimationFrame(step);
}
requestAnimationFrame(step);
}
// ====== STICKY NAV LOGIC ======
const navbar = document.getElementById("navbar");
const mainContent = document.getElementById("main-content");
const heroSection = document.querySelector(".us-hero");
function handleStickyNav() {
if (!navbar || !heroSection) return;
const stickyPoint = heroSection.offsetHeight;
if (window.scrollY >= stickyPoint) {
navbar.classList.add("fixed-nav");
if (mainContent) {
mainContent.style.paddingTop = navbar.offsetHeight + 46 + "px";
}
} else {
navbar.classList.remove("fixed-nav");
if (mainContent) {
mainContent.style.paddingTop = "46px";
}
}
}
// ====== UPDATE: IMPACT COUNTERS (0 → final value animation) ======
// Animates key impact numbers when the Impact section scrolls into view
// Improves clarity and engagement without affecting data accuracy
// Uses IntersectionObserver to trigger once when section enters viewport
window.addEventListener("scroll", handleStickyNav);
window.addEventListener("load", handleStickyNav);
const enterBtn = document.getElementById("enterSiteBtn");
if (enterBtn) {
enterBtn.addEventListener("click", () => {
const target = document.getElementById("main-content") || document.querySelector(".hero");
if (!target) {
console.error("Target section not found!");
return;
}
const y = target.offsetTop - 80;
window.scrollTo({
top: y,
behavior: "smooth"
});
});
}
function animateCount(el, to, duration = 700, suffix = "") {
const start = 0;
const startTime = performance.now();
const easeOut = (t) => 1 - Math.pow(1 - t, 3);
function step(now) {
const t = Math.min(1, (now - startTime) / duration);
const val = Math.round(start + (to - start) * easeOut(t));
el.textContent = val + suffix;
if (t < 1) requestAnimationFrame(step);
}
requestAnimationFrame(step);
}
function setupImpactCounters() {
const section = document.getElementById("impact");
if (!section) return;
const elResponses = document.getElementById("statResponses");
const elReached = document.getElementById("statReached");
const elDonations = document.getElementById("statDonations");
let played = false;
const io = new IntersectionObserver((entries) => {
if (played) return;
if (entries[0].isIntersecting) {
played = true;
// Update: responses statistics number updated (96)
animateCount(elResponses, 96, 650);
// Update: reached statistics number updated (650)
animateCount(elReached, 650, 700);
// 351€
animateCount(elDonations, 351, 800, "€");
io.disconnect();
}
}, { threshold: 0.35 });
io.observe(section);
}
window.addEventListener("load", setupImpactCounters);