Skip to content

Instantly share code, notes, and snippets.

@wowthemesnet
Last active March 8, 2020 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wowthemesnet/a8823b611015e4058b2f3783a0a5a84e to your computer and use it in GitHub Desktop.
Save wowthemesnet/a8823b611015e4058b2f3783a0a5a84e to your computer and use it in GitHub Desktop.
Add random images for multiple img on the same page instead of boring placeholders
var description = [
"https://images.unsplash.com/photo-1561508539-d46a8ac4b20a?ixlib=rb-1.2.1&auto=format&fit=crop&w=675&q=80",
"https://images.unsplash.com/photo-1507914464562-6ff4ac29692f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80",
"https://images.unsplash.com/photo-1565291967637-238f8c5da3de?ixlib=rb-1.2.1&auto=format&fit=crop&w=668&q=80",
"https://images.unsplash.com/photo-1583485267393-510232b60ef7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1341&q=80",
"https://images.unsplash.com/photo-1583356016434-ce82434ed5a1?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80",
"https://images.unsplash.com/photo-1510191240544-c9de9dd0baed?ixlib=rb-1.2.1&auto=format&fit=crop&w=1351&q=80",
"https://images.unsplash.com/photo-1583433348433-b23d79556ee6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80"
];
var size = description.length;
$('img').each(function() {
var x = Math.floor(size * Math.random()); //move random inside loop
if ($(this).hasClass("random")) { //replace "img" with "this"
$(this).attr("src", description[x]);
}
});
<img src="" class="random">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment