Last active
December 22, 2021 21:32
-
-
Save rafibomb/66e6877db3a74752702abd1225b29a2f to your computer and use it in GitHub Desktop.
Randomize Blocks
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
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