From 8ec7f6bd518a7253ccc333e8541c0a2ba5816d03 Mon Sep 17 00:00:00 2001 From: Tom Spilman Date: Sun, 14 Sep 2025 10:03:40 -0500 Subject: [PATCH] Fixed carousel randomization bugs. --- website/content/public/js/carousel.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/website/content/public/js/carousel.js b/website/content/public/js/carousel.js index dc866f3c..83460f25 100644 --- a/website/content/public/js/carousel.js +++ b/website/content/public/js/carousel.js @@ -16,15 +16,24 @@ } const initializeCarousel = () => { + + // Get all the carousel items from the document. let container = document.getElementById('carousel-item-container'); - let items = container.getElementsByClassName('carousel-item'); - const order = Array.from( {length: items.length}, (value, index) => index); - shuffle(order); - for(let i = 0; i < order.length; i++) { - container.appendChild(items[order[i]]); - } - container.children[0].className += ' active'; + let items = Array.from(container.getElementsByClassName('carousel-item')); + + // Remove them before we add them back shuffled. + for(let i = 0; i < items.length; i++) + container.removeChild(items[i]); + + // Since we made a copy we can shuffle it directly. + shuffle(items); + + // Add the items back in the new order. + for(let i = 0; i < items.length; i++) + container.appendChild(items[i]); + // Make the first one visible. + container.children[0].className += ' active'; } initializeCarousel();