-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
79 lines (69 loc) · 2.46 KB
/
script.js
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
// script.js
function centerElement(elementId) {
const element = document.getElementById(elementId);
if (!element) {
console.error(`Element with ID ${elementId} not found.`);
return;
}
const viewportHeight = window.innerHeight;
const viewportWidth = window.innerWidth;
const elementHeight = element.offsetHeight;
const elementWidth = element.offsetWidth;
const placementHeight = (viewportHeight / 2) - (elementHeight / 2);
const placementWidth = (viewportWidth / 2) - (elementWidth / 2);
if (viewportHeight <= 850) {
element.style.position = 'absolute';
element.style.top = `${placementHeight}px`;
element.style.left = `${placementWidth}px`;
} else {
element.style.position = '';
element.style.top = '';
element.style.left = '';
}
}
function bottomElement(elementId) {
const element = document.getElementById(elementId);
if (!element) {
console.error(`Element with ID ${elementId} not found.`);
return;
}
const viewportHeight = window.innerHeight;
const viewportWidth = window.innerWidth;
const elementHeight = element.offsetHeight;
const elementWidth = element.offsetWidth;
const placementHeight = (viewportHeight) - elementHeight * 1.65;
const placementWidth = (viewportWidth / 2) - (elementWidth / 2);
if (viewportHeight <= 850) {
element.style.position = 'absolute';
element.style.display = 'flex';
element.style.top = `${placementHeight}px`;
element.style.left = `${placementWidth}px`;
} else {
element.style.display = 'flex';
element.style.position = 'relative';
element.style.top = '';
element.style.left = '';
}
}
function stickyElement(elementId, mode) {
const element = document.getElementById(elementId);
if (!element) {
console.error(`Element with ID ${elementId} not found.`);
return;
}
if (mode === 'center') {
centerElement(elementId);
window.addEventListener('resize', () => centerElement(elementId));
} else if (mode === 'bottom') {
bottomElement(elementId);
window.addEventListener('resize', () => bottomElement(elementId));
} else {
console.error(`Unsupported mode: ${mode}`);
}
}
stickyElement('mainTitle', 'center');
stickyElement('byLine', 'bottom');
// Smooth scroll to top function
function moveTop() {
window.scrollTo({ top: 0, behavior: 'smooth' });
}