Skip to content

Instantly share code, notes, and snippets.

@BobbyAdamson
Created March 11, 2016 16:09
Show Gist options
  • Save BobbyAdamson/34c5ece1846c034d1e58 to your computer and use it in GitHub Desktop.
Save BobbyAdamson/34c5ece1846c034d1e58 to your computer and use it in GitHub Desktop.
Flip a random media item that's on the page
randomFlipping = function(){
// So we know what to flip
var mediaItem = $('.xItem.xMedialayer.xContentImage, .xItem.xMedialayer.xContentVideo');
// So we know how many items there are
var totalItemsWithMedia = $(mediaItem).length;
// Want to run the function on a somewhat random interval
setInterval(function(){
// Randomly finding an element to flip
var itemNumberToFlip = Math.round(Math.random() * totalItemsWithMedia);
var itemToFlip = $(mediaItem)[itemNumberToFlip];
// Flip the element
$(itemToFlip).find('.xItemInner').addClass('xActive');
// Flip the element back in a few seconds
setTimeout(function(){
$(itemToFlip).find('.xItemInner').removeClass('xActive');
}, (Math.random() * 6) + 6 * 1000);
}, ((Math.random() * 6)) * 500);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment