Skip to content

Instantly share code, notes, and snippets.

@ackernaut
Created August 17, 2016 20:02
Show Gist options
  • Save ackernaut/27c9778ec6cc7ca8a5d1b6c17b794bcd to your computer and use it in GitHub Desktop.
Save ackernaut/27c9778ec6cc7ca8a5d1b6c17b794bcd to your computer and use it in GitHub Desktop.
Get random image from unsplash
// Requires jQuery or Zepto
// Get Random Image (unsplash.it)
var jsRandomImg = $('.js-random-img');
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
jsRandomImg.each(function () {
var imageNumber = getRandomIntInclusive(0, 1084);
$(this).prop('src', 'https://unsplash.it/640/360?image=' + imageNumber);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment