Skip to content

Instantly share code, notes, and snippets.

@ihorduchenko
Last active August 31, 2017 10:54
Show Gist options
  • Save ihorduchenko/c80386aff43a73581ab5e23c1528386d to your computer and use it in GitHub Desktop.
Save ihorduchenko/c80386aff43a73581ab5e23c1528386d to your computer and use it in GitHub Desktop.
Gets random image from image set
<div class="randomImageSet hidden">
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('contact_page_banner_widget')) : else : ?><?php endif; ?>
</div>
<div id="randomImage" class="relative top-bk"></div>
const imgArray = [];
(function slidesArray() {
$(".randomImageSet img").each(function() {
var arrayItem = { image: $(this).attr('src')};
imgArray.push(arrayItem);
});
return imgArray;
}());
var randomArray = imgArray;
(function choosePic() {
var randomNum = Math.floor(Math.random() * randomArray.length);
var img = randomArray[randomNum].image;
var random = document.getElementById("randomImage");
random.style.backgroundImage = 'url(' + img + ')';
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment