Skip to content

Instantly share code, notes, and snippets.

@rafibomb
Last active December 22, 2021 21:32
Show Gist options
  • Save rafibomb/66e6877db3a74752702abd1225b29a2f to your computer and use it in GitHub Desktop.
Save rafibomb/66e6877db3a74752702abd1225b29a2f to your computer and use it in GitHub Desktop.
Randomize Blocks
const teamGrid = document.querySelector('.team-block-grid');
if (teamGrid) {
const teamBlockItems = Array.from(teamGrid.querySelectorAll('.team-block[data-randomize]'));
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1)); // random index from 0 to i
// swap elements array[i] and array[j]
[array[i], array[j]] = [array[j], array[i]];
array[i].setAttribute('data-random-order', i);
}
}
shuffle(teamBlockItems);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment