-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
74 lines (68 loc) · 2.99 KB
/
main.ts
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
import './main.scss';
// Alias for document.getElementById
const EBID: typeof document.getElementById = document.getElementById.bind(document);
// WebP images are only supported on Safari > 13
const isOldSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent) && parseInt(/Version\/([0-9]{2})/.exec(navigator.userAgent)[1]) <= 13;
function scrollListener() {
// Activate menu only after scroll
if (EBID('bio').getBoundingClientRect().bottom < 20) {
EBID('menu').className = 'menu active';
let activeSection: 'passions' | 'creations' | 'activites';
if (EBID('passions').getBoundingClientRect().top < 20) {
activeSection = 'passions';
} else if (EBID('creations').getBoundingClientRect().top < 20) {
activeSection = 'creations';
} else {
activeSection = 'activites';
}
document.querySelectorAll<HTMLButtonElement>('#menu button').forEach(btn => {
if (btn.dataset.link === activeSection) {
btn.className = 'active';
EBID('menu').scrollBy({ left: btn.getBoundingClientRect().left - 10, behavior: 'smooth' });
} else {
btn.className = '';
}
});
} else {
EBID('menu').className = 'menu';
document.querySelectorAll<HTMLButtonElement>('#menu button').forEach(btn => {
btn.className = '';
});
EBID('menu').scrollTo({ left: 0, behavior: 'smooth' });
}
}
function buttonClickListener(e: MouseEvent) {
e.preventDefault();
const target = (e.target as HTMLButtonElement).dataset.link;
console.log(EBID(target));
window.scrollBy({ top: EBID(target).getBoundingClientRect().top - 15, behavior: 'smooth' });
}
function imageClickListener(e: MouseEvent) {
const target = (e.target as HTMLImageElement);
document.querySelector<HTMLImageElement>('#modal img').src = target.src;
document.querySelector<HTMLImageElement>('#modal img').srcset = target.srcset;
document.querySelector<HTMLImageElement>('#modal img').alt = target.alt;
EBID('modal').className = 'modal active';
}
document.addEventListener('DOMContentLoaded', () => {
if (window.scrollY > 0) {
document.body.className = 'skip-animation';
}
document.querySelectorAll<HTMLButtonElement>('#menu button').forEach(btn => {
btn.addEventListener('click', buttonClickListener);
});
document.querySelectorAll<HTMLImageElement>('*:not(.header):not(.modal) > img').forEach(img => {
img.addEventListener('click', imageClickListener, { passive: true });
});
// WebP images are only supported on Safari > 13
if (isOldSafari) {
document.querySelectorAll<HTMLImageElement>('img').forEach(img => {
img.removeAttribute('srcset');
});
}
EBID('modal').addEventListener('click', () => {
EBID('modal').className = 'modal';
});
scrollListener();
}, { passive: true });
document.addEventListener('scroll', scrollListener, { passive: true });