-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
179 lines (166 loc) · 6.6 KB
/
Copy pathscript.js
File metadata and controls
179 lines (166 loc) · 6.6 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
/* =========================================================
Portfolio — Lauri Rekola
Shared behavior: scroll progress, reveal, toast.
Plus: smooth anchor scroll, active topnav highlight,
email copy-to-clipboard.
Vanilla JS, feature-detected.
========================================================= */
(function () {
'use strict';
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
/* ----- Scroll progress bar ----- */
const progressFill = document.querySelector('.scroll-progress__fill');
if (progressFill) {
let ticking = false;
const update = () => {
const max = document.documentElement.scrollHeight - window.innerHeight;
const pct = max > 0 ? Math.min(1, Math.max(0, window.scrollY / max)) : 0;
progressFill.style.transform = 'scaleX(' + pct + ')';
ticking = false;
};
window.addEventListener('scroll', () => {
if (!ticking) {
requestAnimationFrame(update);
ticking = true;
}
}, { passive: true });
update();
}
/* ----- Scroll reveal ----- */
const revealTargets = document.querySelectorAll('.reveal');
if (revealTargets.length && 'IntersectionObserver' in window) {
if (prefersReducedMotion) {
revealTargets.forEach(el => el.classList.add('reveal-done'));
} else {
const io = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('revealed');
io.unobserve(entry.target);
// Replace animation class with static class so hover transitions work normally
setTimeout(() => {
entry.target.classList.replace('revealed', 'reveal-done');
}, 850);
}
});
}, { threshold: 0.12, rootMargin: '0px 0px -40px 0px' });
revealTargets.forEach(el => io.observe(el));
}
}
/* ----- Toast notifications ----- */
const toastHost = document.querySelector('.toast-host');
function toast(message) {
if (!toastHost) return;
const el = document.createElement('div');
el.className = 'toast';
el.textContent = message;
toastHost.appendChild(el);
requestAnimationFrame(() => el.classList.add('toast--in'));
setTimeout(() => {
el.classList.remove('toast--in');
el.addEventListener('transitionend', () => el.remove(), { once: true });
setTimeout(() => el.remove(), 500);
}, 2200);
}
/* ----- Smooth anchor scroll ----- */
document.querySelectorAll('a[href^="#"]').forEach(link => {
link.addEventListener('click', (e) => {
const href = link.getAttribute('href');
if (!href || href === '#') return;
const target = document.querySelector(href);
if (!target) return;
e.preventDefault();
const offset = 70; // topnav-korkeus + pieni puskuri
const top = target.getBoundingClientRect().top + window.scrollY - offset;
window.scrollTo({ top, behavior: prefersReducedMotion ? 'auto' : 'smooth' });
// Päivitä URL-hash ilman jump:ia
history.replaceState(null, '', href);
});
});
/* ----- Active topnav link via scroll position ----- */
const navLinks = document.querySelectorAll('.topnav-links a[href^="#"]');
if (navLinks.length) {
const sections = Array.from(navLinks).map(link => {
const id = link.getAttribute('href').slice(1);
return { id, link, el: document.getElementById(id) };
}).filter(s => s.el);
const mobileSectionLabel = document.querySelector('.mobile-section');
const setActive = (id) => {
navLinks.forEach(a => {
const aId = a.getAttribute('href').slice(1);
if (aId === id) {
a.classList.add('active');
if (mobileSectionLabel) mobileSectionLabel.textContent = a.textContent;
} else {
a.classList.remove('active');
}
});
};
const updateActive = () => {
// At page bottom: always activate last section
if (window.innerHeight + window.scrollY >= document.documentElement.scrollHeight - 4) {
setActive(sections[sections.length - 1].id);
return;
}
// Activate the last section whose top edge is above 35% of viewport height
const threshold = window.innerHeight * 0.35;
let current = sections[0];
for (const s of sections) {
if (s.el.getBoundingClientRect().top <= threshold) current = s;
}
setActive(current.id);
};
window.addEventListener('scroll', updateActive, { passive: true });
updateActive();
}
/* ----- Dynamic footer year ----- */
const yearEl = document.querySelector('.footer-year');
if (yearEl) yearEl.textContent = new Date().getFullYear();
/* ----- Hamburger mobile nav ----- */
const hamburger = document.querySelector('.nav-hamburger');
const mobileNav = document.getElementById('mobile-nav');
if (hamburger && mobileNav) {
const openMenu = () => {
mobileNav.classList.add('open');
hamburger.setAttribute('aria-expanded', 'true');
mobileNav.setAttribute('aria-hidden', 'false');
const firstLink = mobileNav.querySelector('a');
if (firstLink) firstLink.focus();
};
const closeMenu = () => {
if (!mobileNav.classList.contains('open')) return;
mobileNav.classList.remove('open');
hamburger.setAttribute('aria-expanded', 'false');
mobileNav.setAttribute('aria-hidden', 'true');
hamburger.focus();
};
hamburger.addEventListener('click', () => {
mobileNav.classList.contains('open') ? closeMenu() : openMenu();
});
mobileNav.querySelectorAll('a').forEach(link => link.addEventListener('click', closeMenu));
window.addEventListener('resize', () => { if (window.innerWidth > 540) closeMenu(); }, { passive: true });
window.addEventListener('scroll', closeMenu, { passive: true });
document.addEventListener('keydown', e => { if (e.key === 'Escape') closeMenu(); });
}
/* ----- Email copy-to-clipboard ----- */
document.querySelectorAll('[data-copy]').forEach(btn => {
btn.addEventListener('click', async () => {
const value = btn.getAttribute('data-copy');
try {
await navigator.clipboard.writeText(value);
toast('Kopioitu · ' + value);
} catch (err) {
// Fallback vanhempiin selaimiin
const ta = document.createElement('textarea');
ta.value = value;
ta.style.position = 'fixed';
ta.style.opacity = '0';
document.body.appendChild(ta);
ta.select();
try { document.execCommand('copy'); toast('Kopioitu · ' + value); }
catch (e) { toast('Kopiointi ei onnistunut'); }
ta.remove();
}
});
});
})();