Last active
February 7, 2016 00:49
-
-
Save publicarray/71d48e538146cf63099a to your computer and use it in GitHub Desktop.
Background Changer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function BackgroundChanger (settings) { | |
// Author: Sebastian Schmidt | |
var settings = settings || {}; | |
// Set sane default options | |
this.nextImage = settings.firstImage || 1; // image to change to next | |
this.interval = settings.interval || 5000; // time until next image is shown (ms) | |
this.backgroundElement = settings.backgroundElement || document.getElementById('background-sideshow'); // element to change the background image on | |
this.backgroundImages = settings.backgroundImages || [ // a list of image url's | |
'http://lorempixel.com/1280/720/abstract/', | |
'http://lorempixel.com/1280/720/animals/', | |
'http://lorempixel.com/1280/720/nature/', | |
'http://lorempixel.com/1280/720/nightlife/', | |
'http://lorempixel.com/1280/720/city/', | |
]; | |
this.changeBackgroundImage = function() { | |
if (self.nextImage > self.backgroundImages.length - 1) { | |
self.nextImage = 0; | |
} | |
self.backgroundElement.style.backgroundImage = "url(" + self.backgroundImages[self.nextImage] + ")"; | |
self.nextImage++; | |
}; | |
this.start = function() { | |
return setInterval(this.changeBackgroundImage, this.interval); | |
}; | |
var self = this; // allow setInterval etc. to access this scope | |
this.start(); // run | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Background Changer</title> | |
<link href="style.css" rel="stylesheet"> | |
</head> | |
<body> | |
<div id="background-sideshow"></div> | |
<script type="text/javascript"> | |
// Create the Object and set options (everything is optional) | |
var bc = new BackgroundChanger({ | |
firstImage: 1, // image to change to next | |
interval: 5000, // time until next image is shown (ms) | |
backgroundElement: document.querySelector('#background-sideshow'), // element to change the background image on | |
backgroundImages: [ // a list of image url's | |
'http://lorempixel.com/1280/720/abstract/', | |
'http://lorempixel.com/1280/720/animals/', | |
'http://lorempixel.com/1280/720/nature/', | |
'http://lorempixel.com/1280/720/nightlife/', | |
'http://lorempixel.com/1280/720/city/' | |
] | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#background-sideshow { | |
position: fixed; | |
top: 0; | |
left: 0; | |
width: 100%; | |
height: 100%; | |
z-index: -1; | |
background-position: center 0, center; | |
background-repeat: no-repeat; | |
background-size: cover; | |
background-image: url('http://lorempixel.com/1280/720/abstract/'); | |
/* adds a fade transition between images */ | |
transition: background 0.7s linear; | |
-webkit-transition: background 0.7s linear; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment