Skip to content

Commit afbbc35

Browse files
authored
Fixed carousel randomization bugs. (#250)
1 parent ee90790 commit afbbc35

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

website/content/public/js/carousel.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,24 @@
1616
}
1717

1818
const initializeCarousel = () => {
19+
20+
// Get all the carousel items from the document.
1921
let container = document.getElementById('carousel-item-container');
20-
let items = container.getElementsByClassName('carousel-item');
21-
const order = Array.from( {length: items.length}, (value, index) => index);
22-
shuffle(order);
23-
for(let i = 0; i < order.length; i++) {
24-
container.appendChild(items[order[i]]);
25-
}
26-
container.children[0].className += ' active';
22+
let items = Array.from(container.getElementsByClassName('carousel-item'));
23+
24+
// Remove them before we add them back shuffled.
25+
for(let i = 0; i < items.length; i++)
26+
container.removeChild(items[i]);
27+
28+
// Since we made a copy we can shuffle it directly.
29+
shuffle(items);
30+
31+
// Add the items back in the new order.
32+
for(let i = 0; i < items.length; i++)
33+
container.appendChild(items[i]);
2734

35+
// Make the first one visible.
36+
container.children[0].className += ' active';
2837
}
2938

3039
initializeCarousel();

0 commit comments

Comments
 (0)