-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
281 lines (227 loc) · 9.19 KB
/
script.js
File metadata and controls
281 lines (227 loc) · 9.19 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
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
// navbar scrolling
const navbar = document.querySelector('.bar');
const navbarLinks = document.querySelectorAll('.bar a');
window.addEventListener('scroll', function () {
// change navbar color once scrolled down
if (window.scrollY > 50) {
navbar.classList.add('scrolled');
} else {
navbar.classList.remove('scrolled');
}
// for highlighting each section in navbar
let current = '';
// loop through each section with the class `page-section`
document.querySelectorAll('.page-section').forEach(section => {
const sectionTop = section.offsetTop;
if (window.scrollY >= sectionTop - 50) {
current = section.getAttribute('id');
}
});
// remove active class from all links
navbarLinks.forEach(link => {
link.parentElement.classList.remove('active');
});
// check if user scrolled to the bottom
if (window.innerHeight + window.scrollY >= document.body.offsetHeight) { // add active class to mailto link
document.querySelector('.bar a[href^="mailto:"]').parentElement.classList.add('active');
} else { // add active class to current link
document.querySelector(`.bar a[href*="${current}"]`).parentElement.classList.add('active');
}
});
// for weather in description
document.addEventListener('DOMContentLoaded', function() {
const apiKey = 'd03e81b7e6e7449d91a103336242507';
const city = 'Chicago';
const apiUrl = `https://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${city}`;
fetch(apiUrl)
.then(response => response.json())
.then(data => {
const weatherElement = document.querySelector('.weather');
const temperature = data.current.temp_c;
weatherElement.textContent = `${temperature}°C`;
})
.catch(error => { // if error, log to console
console.error('Error fetching weather data:', error);
});
});
// projects slider
// previous slide
document.querySelector('.carousel-control.prev').addEventListener('click', function() {
const checked = document.querySelector('input[name="slider"]:checked');
if (checked) {
const allSliders = document.querySelectorAll('input[name="slider"]');
const index = Array.from(allSliders).indexOf(checked);
const prevIndex = (index - 1 + allSliders.length) % allSliders.length;
allSliders[prevIndex].checked = true;
} else { // log error
console.error('No checked input found.');
}
});
// next slide
document.querySelector('.carousel-control.next').addEventListener('click', function() {
const checked = document.querySelector('input[name="slider"]:checked');
if (checked) {
const allSliders = document.querySelectorAll('input[name="slider"]');
const index = Array.from(allSliders).indexOf(checked);
const nextIndex = (index + 1) % allSliders.length;
allSliders[nextIndex].checked = true;
} else { //log error
console.error('No checked input found.');
}
});
// pick travel image size from Imgur
function updateImageSources() {
const imagesLarge = document.querySelectorAll('.large-img');
const imagesSmall = document.querySelectorAll('.small-img');
const screenWidth = window.innerWidth;
imagesLarge.forEach(img => {
const baseSrc = img.getAttribute('data-base-src');
const extension = baseSrc.split('.').pop(); // Get the file extension
const baseName = baseSrc.replace(`.${extension}`, ''); // Get the base filename without extension
let suffix = '';
if (screenWidth > 335){
suffix = 'h'; // Imgur uses a suffix of h to indicate huge image (longest dimension of 1024px)
} else if (screenWidth <= 335) {
suffix = 'l'; // suffix l to indicate medium
}
img.src = `${baseName}${suffix}.${extension}`;
});
imagesSmall.forEach(img => {
const baseSrc = img.getAttribute('data-base-src');
const extension = baseSrc.split('.').pop(); // Get the file extension
const baseName = baseSrc.replace(`.${extension}`, ''); // Get the base filename without extension
let suffix = '';
if (screenWidth > 768) {
suffix = 'h';
} else if (screenWidth <= 768) {
img.src = '';
return;
}
img.src = `${baseName}${suffix}.${extension}`;
});
}
// update img source
window.addEventListener('load', updateImageSources);
window.addEventListener('resize', updateImageSources);
// shuffle travel imgs at load
document.addEventListener("DOMContentLoaded", function() {
// function to shuffle an array
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
// get all slider containers
const sliders = document.querySelectorAll('[data-slider]');
sliders.forEach(slider => {
const slides = Array.from(slider.querySelectorAll('ul[data-slides] > li.slide'));
// shuffle slides
shuffle(slides);
// clear existing slides in the container
const slidesContainer = slider.querySelector('ul[data-slides]');
slidesContainer.innerHTML = '';
// append shuffled slides back to the container
slides.forEach((slide, index) => {
if (index === 0) {
slide.setAttribute('data-active', ''); // add data-active to the first slide
}
slidesContainer.appendChild(slide);
});
});
});
// travel images slider
const buttons = document.querySelectorAll("[data-slider-button]")
buttons.forEach(button => {
button.addEventListener("click", () => {
const offset = button.dataset.sliderButton === "next" ? 1 : -1
const slides = button
.closest("[data-slider]")
.querySelector("[data-slides]")
const activeSlide = slides.querySelector("[data-active]")
let newIndex = [...slides.children].indexOf(activeSlide) + offset
if (newIndex < 0) newIndex = slides.children.length - 1
if (newIndex >= slides.children.length) newIndex = 0
slides.children[newIndex].dataset.active = true
delete activeSlide.dataset.active
})
})
// figcaption of images = alt text
document.querySelectorAll('.slide').forEach(slide => {
const img = slide.querySelector('img'); // find the image within the current slide
if (img && img.alt) { // check if the image exists and has an alt attribute
// create a figcaption element
const figcaption = document.createElement('figcaption');
figcaption.textContent = img.alt;
slide.appendChild(figcaption);
}
});
// slides change automatically if not hovered over
// slide intervals
const LARGE_SLIDER_INTERVAL_TIME = 12000; // 12 seconds
const SMALL_SLIDER_INTERVAL_TIME = 10000; // 10 seconds
document.addEventListener('DOMContentLoaded', () => {
const largeSlider = document.querySelector('.slider-container.large');
const smallSlider = document.querySelector('.slider-container.small');
let largeSliderInterval;
let smallSliderInterval;
function startAutoSlide(slider, intervalTime) {
return setInterval(() => {
const activeSlide = slider.querySelector('[data-active]');
const nextSlide = activeSlide.nextElementSibling || slider.querySelector('.slide:first-child');
activeSlide.removeAttribute('data-active');
nextSlide.setAttribute('data-active', true);
}, intervalTime);
}
function stopAutoSlide(interval) {
clearInterval(interval);
}
// initialize auto slides
largeSliderInterval = startAutoSlide(largeSlider, LARGE_SLIDER_INTERVAL_TIME);
smallSliderInterval = startAutoSlide(smallSlider, SMALL_SLIDER_INTERVAL_TIME);
// add event listeners to stop auto-slide on hover
largeSlider.addEventListener('mouseover', () => {
stopAutoSlide(largeSliderInterval);
});
largeSlider.addEventListener('mouseout', () => {
largeSliderInterval = startAutoSlide(largeSlider, LARGE_SLIDER_INTERVAL_TIME);
});
smallSlider.addEventListener('mouseover', () => {
stopAutoSlide(smallSliderInterval);
});
smallSlider.addEventListener('mouseout', () => {
smallSliderInterval = startAutoSlide(smallSlider, SMALL_SLIDER_INTERVAL_TIME);
});
});
// loading page
document.addEventListener('DOMContentLoaded', () => {
// get elements
const hiKevin = document.querySelector('#about .title');
const introduction = document.querySelector('#about .introduction');
const restOfContent = document.querySelectorAll('.page-section:not(#about)');
// function to reveal elements
const revealElements = () => {
hiKevin.classList.add('visible'); // add 'visible' class to first section
setTimeout(() => {
introduction.classList.add('visible'); // add 'visible' class to introduction
setTimeout(() => {
restOfContent.forEach(section => section.classList.add('visible')); // add 'visible' class to rest of sections
}, 50); // 50ms between last two parts
}, 200);
};
// trigger the reveal function
revealElements();
});
// change title according to screen size
document.addEventListener('DOMContentLoaded', () => {
const titleElement = document.querySelector('.title');
function updateTitleText() {
if (window.innerWidth <= 900) {
titleElement.textContent = "I'm Kevin.";
} else {
titleElement.textContent = "Hi, I'm Kevin.";
}
}
window.addEventListener('resize', updateTitleText);
updateTitleText();
});