-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
154 lines (137 loc) · 4.39 KB
/
index.js
File metadata and controls
154 lines (137 loc) · 4.39 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
// CONSTANTS
const section_btn_ids = [
"about-me-btn", "experience-btn", "projects-btn", "contact-btn"
]
const section_ids = [
"about-me", "experience", "projects", "contact"
]
const sec_map = new Map();
for (let i = 0; i < section_btn_ids.length; i++) {
sec_map.set(section_btn_ids[i], section_ids[i]);
}
let isAnimating = true;
// ON PAGE INITIAL LOAD
const header = document.querySelector('.header')
const snow_julia = document.getElementById("snow-julia");
const julia_sitting = document.getElementById("julia-sitting");
const experience_items = document.querySelectorAll('.experience-item');
const section_btns = [...document.querySelectorAll('.section-btn')];
const sections = [...document.querySelectorAll('.section')];
const intro_btn = document.getElementById("intro-btn");
let active_sec = document.getElementById("intro");
let active_btn = null;
let active_item = null;
// NAVIGATION
intro_btn.addEventListener("click", () => {
if (active_btn) {
active_btn.classList.remove("active-section");
active_btn = null;
}
window.scrollTo({top: 0, behavior: "smooth"});
})
section_btns.forEach((sec) => {
sec.addEventListener('click', activateSection);
})
// INTERSECTION OBSERVER
let direction = 'up';
let prevYPosition = 0;
function shouldUpdate(entry, direction) {
return (entry.isIntersecting && direction === 'up') ||
(!entry.isIntersecting && direction === 'down');
}
const callback = (entries) => {
entries.forEach((entry) => {
if (window.scrollY > prevYPosition) {
direction = 'down';
} else {
direction = 'up';
}
prevYPosition = window.scrollY;
// if down and true, then activate target
if (direction === 'down' && entry.isIntersecting) {
const button_id = entry.target.id + "-btn";
const button = document.getElementById(button_id);
activateSectionButton(button);
} else if (direction === 'up' && entry.isIntersecting) {
const button_id = entry.target.id + "-btn";
const button = document.getElementById(button_id);
activateSectionButton(button);
}
})
}
const options = {
rootMargin: "-75px",
threshold: 0
}
if (window.innerWidth > 1024) {
const observer = new IntersectionObserver(callback, options);
sections.forEach((sec) => {
observer.observe(sec);
})
}
// FORM SUBMISSION
const form = document.getElementById('form');
const result = document.getElementById('result');
form.addEventListener('submit', function(e) {
e.preventDefault();
const formData = new FormData(form);
const object = Object.fromEntries(formData);
const json = JSON.stringify(object);
result.innerHTML = "Please wait..."
fetch('https://api.web3forms.com/submit', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: json
})
.then(async (response) => {
let json = await response.json();
if (response.status == 200) {
result.innerHTML = "Form submitted successfully";
} else {
console.log(response);
result.innerHTML = json.message;
}
})
.catch(error => {
console.log(error);
result.innerHTML = "Something went wrong!";
})
.then(function() {
form.reset();
setTimeout(() => {
result.style.display = "none";
}, 3000);
});
});
// ANIMATE SNOW_JULIA
animate_snow_julia();
/*-------------------- FUNCTIONS ------------------ */
function animate_snow_julia() {
snow_julia.animate(
[
{ transform: 'translateX(150%)' },
{ transform: 'translateX(0%)' }
],
{
duration: 500,
iterations: 1
}
)
}
// happens on click
function activateSection(e) {
activateSectionButton(e.target);
// scroll into view
const section_target = document.getElementById(sec_map.get(active_btn.id));
section_target.scrollIntoView({behavior: "smooth"});
}
function activateSectionButton(section_btn) {
// deactivate currently active button
active_btn?.classList.remove("active-section");
// set new active button
active_btn = section_btn;
active_btn.classList.add("active-section");
}