Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save ki11ua/24852452272065fb5a00bab9eda5af27 to your computer and use it in GitHub Desktop.
Save ki11ua/24852452272065fb5a00bab9eda5af27 to your computer and use it in GitHub Desktop.
Super simple jQuery background-image serial 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 center;
background-size: cover;
-webkit-transition: all 0.7s ease;
-moz-transition: all 0.7s ease;
-ms-transition: all 0.7s ease;
-o-transition: all 0.7s ease;
transition: all 0.7s ease;
}
/* Use additional CSS to control the `height` of `[data-slides]`, like so: */
.test { height: 300px; }
@media all and (min-width: 48em) {
.test { height: 400px; }
}
<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 -->
!function(t){"use strict";var a=t("[data-slides]"),ni=0,s=a.data("slides"),e=s.length,n=function(){if(ni>=a.length){ni=0}else{ni++};a.css("background-image",'url("'+s[ni]+'")').show(0,function(){setTimeout(n,5e3)})};n()}(jQuery);
@rameezwp
Copy link

rameezwp commented May 1, 2016

Hi, I tried it and I think it has some problem. Sometimes it keeps repeating 2 images rather than all of them.
Any suggestion?

@renatorocha1
Copy link

renatorocha1 commented Aug 10, 2016

using two data-slides on the same page? Thank you.

@Ema4rl
Copy link

Ema4rl commented Aug 25, 2016

@rameez-nmedia, I created a fork which solves the issue and includes a nice image preload trick @ssbg-slideshow.js
I also had the issue with 3 or more images and had to rewrite the javascript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment