Skip to content

Instantly share code, notes, and snippets.

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 rooven/29754072d7f28a99c149 to your computer and use it in GitHub Desktop.
Save rooven/29754072d7f28a99c149 to your computer and use it in GitHub Desktop.
Super simple jQuery background-image random slideshow (using CSS3 for the transition and gets the image paths via HTML5 `data` attribute).
[data-slides] {
background-image: url(../../uploads/banner1.jpg); /* Default image. */
background-repeat: no-repeat;
background-position: center top;
background-size: cover;
transition: background-image 1s linear;
}
/* Use additional CSS to control the `height` of `[data-slides]`, like so: */
.test { height: 220px; }
@media all and (min-width: 48em) {
.test { height: 320px; }
}
<div
class="test"
data-slides='[
"uploads/banner1.jpg",
"uploads/banner2.jpg",
"uploads/banner3.jpg",
"uploads/banner4.jpg",
"uploads/banner5.jpg",
"uploads/banner6.jpg",
"uploads/banner7.jpg",
"uploads/banner8.jpg",
"uploads/banner9.jpg"
]'
> … </div> <!-- /.primary -->
/*! slides | https://gist.github.com/mhulse/66bcbb7099bb4beae530 */
(function($) {
'use strict';
var $slides = $('[data-slides]');
var images = $slides.data('slides');
var count = images.length;
var slideshow = function() {
$slides
.css('background-image', 'url("' + images[Math.floor(Math.random() * count)] + '")')
.show(0, function() {
setTimeout(slideshow, 5000);
});
};
slideshow();
}(jQuery));
/*! slides | https://gist.github.com/mhulse/66bcbb7099bb4beae530 */
!function(t){"use strict";var a=t("[data-slides]"),s=a.data("slides"),e=s.length,n=function(){a.css("background-image",'url("'+s[Math.floor(Math.random()*e)]+'")').show(0,function(){setTimeout(n,5e3)})};n()}(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment