Skip to content

Instantly share code, notes, and snippets.

@Ema4rl
Forked from ki11ua/Super simple background image slideshow.css
Last active January 18, 2020 18:43
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Ema4rl/b8ef90be99205ddada5ef2cd6e632ebe to your computer and use it in GitHub Desktop.
Save Ema4rl/b8ef90be99205ddada5ef2cd6e632ebe to your computer and use it in GitHub Desktop.
Super Simple jQuery Background-image Slideshow (using CSS3 for the transition, gets the image paths via HTML5 `data` attribute, and with image preload trick).
/*!
* By Eharry.me (https://gist.github.com/Ema4rl/b8ef90be99205ddada5ef2cd6e632ebe)
*/
! function ($) {
"use strict";
var slide = $("[data-slides]"),
count = 0,
slides = slide.data("slides"),
len = slides.length,
n = function () {
if (count >= len) {
count = 0
}
slide.css("background-image", 'url("' + slides[count] + '")').show(0, function () {
setTimeout(n, 5e3)
});
count++;
};
n()
}(jQuery);
[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; }
}
/* additional CSS for hidden image preload section */
[hidden] { display: none; }
<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 -->
<!-- image preload (not compulsory) -->
<div hidden>
<img src="uploads/banner1.jpg">
<img src="uploads/banner2.jpg">
<img src="uploads/banner3.jpg">
<img src="uploads/banner4.jpg">
<img src="uploads/banner5.jpg">
<img src="uploads/banner6.jpg">
<img src="uploads/banner7.jpg">
<img src="uploads/banner8.jpg">
<img src="uploads/banner9.jpg">
</div> <!-- /.image preload -->
/*!
* By Eharry.me (https://gist.github.com/Ema4rl/b8ef90be99205ddada5ef2cd6e632ebe)
*/
!function(a){"use strict";var b=a("[data-slides]"),c=0,d=b.data("slides"),e=d.length,f=function(){c>=e&&(c=0),b.css("background-image",'url("'+d[c]+'")').show(0,function(){setTimeout(f,5e3)}),c++};f()}(jQuery);
@Ema4rl
Copy link
Author

Ema4rl commented Aug 25, 2016

You can automatically load this plugin with: $O.test('[data-slides]').js('ssbg-slideshow.min.js'); using Only.js!

@khomel
Copy link

khomel commented May 24, 2017

For some reason this is only animating in Chome and Safari. No Firefox, IE, or Edge. Is there something I'm missing?

@JamSameer
Copy link

This error is showing. what to do now

Uncaught TypeError: Cannot read property 'length' of null .

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