website/assets/js/main.js

67 lines
2.3 KiB
JavaScript
Raw Permalink Normal View History

2023-10-24 16:47:07 +00:00
const collapsibleButton = document.querySelector("#hamburger-menu");
const menuContent = document.querySelector(".menu-content");
collapsibleButton.addEventListener("click", function () {
menuContent.classList.toggle("active");
if (menuContent.style.display === "block") {
menuContent.style.display = "none";
} else {
menuContent.style.display = "block";
}
});
2023-12-25 03:06:08 +00:00
document.addEventListener('DOMContentLoaded', function () {
2024-03-29 05:40:57 +00:00
if (window.location.pathname === "/index.html") {
2024-04-23 15:52:01 +00:00
fetch('/assets/js/carousel-items.json')
2024-03-29 04:13:45 +00:00
.then(response => response.json())
.then(data => {
createCarouselItems(data);
initializeCarousel();
})
.catch(error => console.error('Error loading JSON:', error));
}
2023-12-25 03:06:08 +00:00
});
function createCarouselItems(items) {
2023-12-28 18:42:30 +00:00
const carousel = document.querySelector('#carousel');
2023-12-25 03:06:08 +00:00
items.forEach(item => {
const itemDiv = document.createElement('div');
2023-12-28 18:42:30 +00:00
itemDiv.className = 'carousel-item'
const link = document.createElement('a');
link.className = 'carousel-link';
link.href = item.link;
link.target = '_blank';
link.rel = 'noopener noreferrer';
const linkText = document.createTextNode(item.description);
link.appendChild(linkText);
itemDiv.appendChild(link);
2023-12-25 03:06:08 +00:00
carousel.appendChild(itemDiv);
});
}
function initializeCarousel() {
2023-12-28 18:42:30 +00:00
const carousel = document.querySelector('#carousel');
2023-12-25 03:06:08 +00:00
const items = Array.from(carousel.children);
const totalItems = items.length;
const middleIndex = Math.floor(totalItems / 2);
let currentIndex = -middleIndex;
function cycleItems() {
currentIndex = (currentIndex - 1 + totalItems) % totalItems;
updateCarouselItems();
}
function updateCarouselItems() {
items.forEach((item, index) => {
let positionIndex = (currentIndex + index + totalItems) % totalItems;
let offset = positionIndex - middleIndex;
2023-12-28 18:42:30 +00:00
item.style.transform = `translateY(${offset * 100}%)`;
2023-12-25 03:06:08 +00:00
item.classList.toggle('active', positionIndex === middleIndex);
2023-12-28 18:42:30 +00:00
item.style.visibility = 'visible';
2023-12-25 03:06:08 +00:00
});
}
updateCarouselItems();
2023-12-28 18:42:30 +00:00
setInterval(cycleItems, 7000);
2023-12-25 03:06:08 +00:00
}