-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
305 lines (252 loc) · 7.58 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
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
"use strict";
// Selectors:
// Setions:
const section1 = document.querySelector("#section--1");
const allSections = document.querySelectorAll(".section");
// Navbars:
const nav = document.querySelector(".nav");
const navLinks = document.querySelector(".nav__links");
const footerLinks = document.querySelector(".footer__nav__list");
const menus = document.querySelectorAll(".menu");
// Others selectors:
const scrollDownImg = document.querySelector(".scroll__img");
const dotContainer = document.querySelector(".dots");
// Navbars
const navbarBehaviour = function () {
// Smooth Scrolling:
const smoothScrollHandler = function (e, target) {
e.preventDefault();
if (e.target.classList.contains(`${target}`)) {
const id = e.target.getAttribute("href");
document.querySelector(id).scrollIntoView({ behavior: "smooth" });
}
};
// Top navbar
navLinks.addEventListener("click", (e) => {
smoothScrollHandler(e, "nav__link");
});
// Bottom navbar
footerLinks.addEventListener("click", (e) => {
smoothScrollHandler(e, "footer__link");
});
// Menu fade out animation:
const fadeOutHandler = function (e, opacity) {
if (
e.target.classList.contains("nav__link") ||
e.target.classList.contains("footer__link")
) {
// select (logo, siblings)
const others = document.querySelectorAll(".fade__out");
others.forEach((other) => {
if (other !== e.target) {
other.style.opacity = opacity;
}
});
}
};
menus.forEach((menu) => {
menu.addEventListener("mouseover", function (e) {
fadeOutHandler(e, 0.5);
});
menu.addEventListener("mouseout", function (e) {
fadeOutHandler(e, 1);
});
});
};
///////////////////////////////////
// Sticky Navigation:
const stickyNavFunction = function () {
const stickyNav = function (entries) {
entries.forEach((entry) => {
if (!entry.isIntersecting) {
nav.classList.add("sticky");
scrollDownImg.style.opacity = 0;
}
if (entry.isIntersecting) {
nav.classList.remove("sticky");
scrollDownImg.style.opacity = 1;
}
});
};
const section1Observer = new IntersectionObserver(stickyNav, {
root: null,
threshold: 0.2,
// TODO: Calculate rootMargin dynamically using .getBoundClientRect()
rootMargin: "30px",
});
section1Observer.observe(section1);
};
///////////////////////////////////
// Sections reveal
// 0. hide sections
// 1. section observer
// 2. for each section reveal current section
const sectionsReveal = function () {
allSections.forEach((section) => {
section.classList.add("section--hidden");
});
const revealSection = function (entries, observer) {
const [entry] = entries;
if (!entry.isIntersecting) return;
entry.target.classList.remove("section--hidden");
observer.unobserve(entry.target);
};
const sectionAllObserver = new IntersectionObserver(revealSection, {
root: null,
threshold: 0,
rootMargin: "0px",
});
allSections.forEach((section) => {
sectionAllObserver.observe(section);
});
};
///////////////////////////////////
// Project slider
const slider = function () {
const slides = document.querySelectorAll(".project");
const bttnLeft = document.querySelector(".slider__btn--left");
const bttnRight = document.querySelector(".slider__btn--right");
let curSlide = 0;
const maxSlide = slides.length - 1;
// Dots
const createDots = function () {
slides.forEach(function (_, i) {
dotContainer.insertAdjacentHTML(
"beforeend",
`<button class="dots__dot" data-slide="${i}"></button>`
);
});
};
createDots();
const activeDot = function () {
slides.forEach((slide) => {
if (slide.style.transform === "translateX(0%)") {
const slideNumber = slide.classList.value.slice(-1);
document.querySelectorAll(".dots__dot").forEach((dot) => {
if (dot.dataset.slide === `${slideNumber - 1}`) {
dot.classList.add("dot__active");
} else dot.classList.remove("dot__active");
});
}
});
};
const goToSlide = function (slide) {
slides.forEach((s, i) => {
// slide 0: 0%, 100%, 200%
s.style.transform = `translateX(${100 * (slide + i)}%)`;
activeDot();
});
};
goToSlide(curSlide);
const nextSlide = function () {
if (curSlide === maxSlide * -1) {
curSlide = 0;
} else {
curSlide--;
}
goToSlide(curSlide);
};
const prevSlide = function () {
if (curSlide === 0) {
curSlide = maxSlide * -1;
} else curSlide++;
goToSlide(curSlide);
};
bttnLeft.addEventListener("click", prevSlide);
bttnRight.addEventListener("click", nextSlide);
document.addEventListener("keydown", function (e) {
e.preventDefault();
if (e.key === "ArrowLeft") prevSlide();
if (e.key === "ArrowRight") nextSlide();
});
dotContainer.addEventListener("click", function (e) {
e.preventDefault();
// matching strategy
if (!e.target.classList.contains("dots__dot")) return;
curSlide = e.target.dataset.slide * -1;
goToSlide(curSlide);
});
};
///////////////////
// Toggl API hours spent programming
const getTime = function () {
const url = "https://koderad-api.onrender.com/toggl";
async function getTimeLearning() {
try {
const response = await fetch(url, {
method: "GET",
});
const hours = await response.json();
// DOM Manipulation
document.getElementById(
"skills_description"
).textContent = `I have spent ${hours} hours (current time form API!) learning mainly JavaScript along with
HTML and CSS. Thanks to that commitment I believe I will bring a lot
to the table if we ever met business-wise.`;
} catch (error) {
console.error(error);
}
}
getTimeLearning();
};
///////////////////////////////////
// Chuck Norris
const chuckAPI = async function () {
const url = "https://api.chucknorris.io/jokes/random";
// Helper function for banned words
function checkForBan(array, words) {
const result = array.some((el) => words.includes(el));
return result;
}
try {
const response = await fetch(url);
const result = await response.json();
const joke = result.value;
const jokeArr = joke.split(" ");
const bannedWords = [
"black", "sperm", "rectum", "dick", "urine", "semen", "penis", "whore", "whores", "pussy", "racism", "mother", 'bitch', 'woman', 'cock', 'women', 'cum']; // prettier-ignore
// guard clauses for banned words/length
if (checkForBan(jokeArr, bannedWords) && jokeArr.length > 37) chuckAPI();
document.querySelector(".joke").textContent = `"${joke}"`;
return result.value;
} catch (error) {
console.error(error);
}
};
document.querySelector(".btn__joke").addEventListener("click", function (e) {
e.preventDefault();
chuckAPI();
});
function handleLargeScreen() {
navbarBehaviour();
stickyNavFunction();
sectionsReveal();
slider();
chuckAPI();
}
function handleSmallScreen() {
sectionsReveal();
slider();
chuckAPI();
}
// Check screen size on page load and resize
function checkScreenSize() {
if (window.innerWidth <= 901) {
handleSmallScreen();
} else {
handleLargeScreen();
}
}
// Call checkScreenSize on page load
window.addEventListener("load", checkScreenSize);
// Call checkScreenSize on window resize DEBOUNCING
function debounce(callback, delay) {
let timeoutId;
return function () {
clearTimeout(timeoutId);
timeoutId = setTimeout(callback, delay);
};
}
window.addEventListener("resize", function () {
debounce(checkScreenSize, 500);
});