-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0af61db
commit 58448e8
Showing
3 changed files
with
47 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,47 @@ | ||
const carouselList = document.getElementById('carouselList'); | ||
const items = carouselList.getElementsByClassName('carousel-item'); | ||
const itemHeight = items[0].offsetHeight; | ||
const totalItems = items.length; | ||
let currentIndex = 0; | ||
// JavaScript for autoscrolling all carousels simultaneously | ||
const carouselContainers = document.querySelectorAll('.carousel-container'); | ||
const carousels = []; | ||
|
||
function scrollCarousel() { | ||
currentIndex++; | ||
if (currentIndex === totalItems) { | ||
currentIndex = 0; | ||
carouselContainers.forEach(container => { | ||
const carouselList = container.querySelector('.carousel-list'); | ||
const items = carouselList.getElementsByClassName('carousel-item'); | ||
const itemHeight = items[0].offsetHeight; | ||
const totalItems = items.length; | ||
let currentIndex = 0; | ||
let isResetting = false; | ||
|
||
// Push each carousel object into an array | ||
carousels.push({ | ||
carouselList, | ||
itemHeight, | ||
totalItems, | ||
currentIndex, | ||
isResetting | ||
}); | ||
}); | ||
|
||
function scrollAllCarousels() { | ||
carousels.forEach(carousel => { | ||
carousel.currentIndex++; | ||
if (carousel.currentIndex === carousel.totalItems) { | ||
carousel.isResetting = true; | ||
carousel.currentIndex = 0; | ||
carousel.carouselList.style.transition = 'top 0.5s ease-in-out'; // Enable transition for smooth scrolling | ||
carousel.carouselList.style.top = `${-carousel.itemHeight}px`; // Start scroll animation | ||
} | ||
if (carousel.isResetting && carousel.currentIndex === 0) { | ||
// If resetting and back to the top, reset the top position instantly without animation | ||
carousel.isResetting = false; | ||
carousel.carouselList.style.transition = 'none'; // Disable transition for instant reset | ||
carousel.carouselList.style.top = '0px'; // Reset to the top instantly | ||
setTimeout(() => { | ||
carousel.carouselList.style.transition = ''; // Re-enable transition | ||
}, 50); // Add a slight delay before re-enabling transition to ensure it takes effect | ||
} | ||
const displacement = -currentIndex * itemHeight; | ||
carouselList.style.top = displacement + 'px'; | ||
const displacement = -carousel.currentIndex * carousel.itemHeight; | ||
carousel.carouselList.style.top = `${displacement}px`; | ||
}); | ||
} | ||
|
||
// Set interval for autoscrolling (adjust timing as needed) | ||
const scrollInterval = setInterval(scrollCarousel, 2000); | ||
// Set interval for autoscrolling all carousels simultaneously | ||
setInterval(scrollAllCarousels, 3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters