-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
173 lines (138 loc) · 5.75 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
// ----- CAROUSEL AUTOSCROLLING -----
// ----- Currently not in use -----
/*
const carouselContent = document.querySelector('.carousel');
const carouselItems = document.querySelectorAll('.carousel-item');
// Calculate item width only once
const itemWidth = carouselContent.offsetWidth;
let autoScrollInterval;
function startAutoScroll() {
autoScrollInterval = setInterval(() => {
const currentScrollLeft = carouselContent.scrollLeft;
const lastItemRight = carouselItems[carouselItems.length - 1].offsetLeft + itemWidth;
if (currentScrollLeft + carouselContent.offsetWidth >= lastItemRight) {
carouselContent.scrollTo({ left: 0, behavior: 'smooth' });
} else {
carouselContent.scrollBy({ left: 20, behavior: 'smooth' });
}
}, 3000); // Adjust interval duration
}
function stopAutoScroll() {
clearInterval(autoScrollInterval);
}
carouselContent.addEventListener('mouseenter', stopAutoScroll);
carouselContent.addEventListener('mouseleave', startAutoScroll);
// Start auto-scrolling when the page loads
startAutoScroll();
*/
// ----- POPUP FUNCTIONALITY -----
var popup = document.getElementById("popup");
var span = document.getElementsByClassName("close")[0];
var dontShowAgain = document.getElementById("dontShowAgain");
// Load previous checkbox state from localStorage
if (localStorage.getItem('dontshowPopup') === 'true') {
popup.style.display = "none";
} else {
popup.style.display = "flex";
}
// Function to close the popup
function closePopup() {
popup.style.display = "none";
localStorage.setItem('dontshowPopup', dontShowAgain.checked); // Store the correct state
}
span.onclick = closePopup;
window.onclick = function (event) {
if (event.target == popup) {
closePopup();
}
};
// ----- ON SCROLL FUNCTIONS -----
window.addEventListener('scroll', function () {
const logo = document.querySelector('.logo');
const topRow = document.querySelector('.topRow');
const scrollPosition = window.scrollY;
logo.classList.toggle('logo-onScroll', scrollPosition > 0);
topRow.classList.toggle('topRow-onScroll', scrollPosition > 0);
// ----- FAVICON FUNCTIONALITY ----- (Moved inside the scroll event)
const faviconLinks = document.querySelectorAll('link[rel="icon"]');
const colorSchemeQuery = window.matchMedia('(prefers-color-scheme: dark)');
function updateFavicon() {
faviconLinks.forEach(link => {
link.disabled = !(colorSchemeQuery.matches === (link.media === '(prefers-color-scheme: dark)'));
});
}
updateFavicon();
});
// ----- CAROUSEL FUNCTIONALITY -----
const carouselContent = document.querySelector('.carousel');
const carouselItems = carouselContent.querySelectorAll('.carousel-item');
// Calculate item width only once
const itemWidth = carouselItems[0].offsetWidth;
// Scroll to a specific carousel item
function scrollCarouselTo(index) {
const targetScrollLeft = carouselItems[index].offsetLeft;
carouselContent.scrollTo({ left: targetScrollLeft, behavior: 'smooth' });
}
// Event Listeners for Carousel Buttons
document.getElementById('prev-projects').addEventListener('click', () => {
const currentItemIndex = Math.round(carouselContent.scrollLeft / itemWidth);
const targetIndex = (currentItemIndex === 0) ? carouselItems.length - 1 : currentItemIndex - 1;
scrollCarouselTo(targetIndex);
});
document.getElementById('next-projects').addEventListener('click', () => {
const currentItemIndex = Math.round(carouselContent.scrollLeft / itemWidth);
const targetIndex = (currentItemIndex === carouselItems.length - 1) ? 0 : currentItemIndex + 1;
scrollCarouselTo(targetIndex);
});
// ----- SMOOTH SCROLL TO TARGET FUNCTION -----
function adjustScrollPosition(targetId) {
const targetElement = document.getElementById(targetId);
if (targetElement) {
const topRowHeight = document.querySelector('.topRow').offsetHeight;
const adjustedScrollTop = targetElement.getBoundingClientRect().top + window.pageYOffset - topRowHeight;
window.scrollTo({ top: adjustedScrollTop, behavior: 'smooth' });
// Scroll snap adjustment (if needed)
setTimeout(() => {
const scrollSnapDelta = window.scrollY % document.documentElement.clientHeight;
if (scrollSnapDelta > 0) {
window.scrollBy({ top: -scrollSnapDelta });
}
}, 100); // Adjust delay as needed
}
}
// --- Get GitHub Repo Info ---
async function getGitHubRepoInfo(repoLink) {
try {
const repoUrlParts = repoLink.split('/');
const repoUrl = `https://api.github.com/repos/${repoUrlParts[3]}/${repoUrlParts[4]}`;
const response = await fetch(repoUrl);
const data = await response.json();
console.log("fetched data:", data);
return {
name: data.name || "Error fetching",
description: data.description || "No description or/and name available"
};
} catch (error) {
console.error("Error fetching GitHub data:", error);
return {
name: "Error fetching name",
description: "Error fetching description"
};
}
}
// Function to update carousel content based on GitHub data
async function updateCarouselWithGitHubData() {
const carouselLeftContents = document.querySelectorAll('.carousel-left-content');
for (const content of carouselLeftContents) {
const linkElement = content.querySelector('a'); // Find the link element (if it exists)
if (linkElement && linkElement.href.includes('github.com')) {
const repoInfo = await getGitHubRepoInfo(linkElement.href);
const titleElement = content.querySelector('.project-title');
const textElement = content.querySelector('.project-text');
if (titleElement) titleElement.textContent = repoInfo.name;
if (textElement) textElement.textContent = repoInfo.description;
}
}
}
// Call the update function when the page loads
updateCarouselWithGitHubData();