Last active
June 10, 2016 16:26
-
-
Save mikekavouras/d617217cc74d3016b39f58d57ca2f4ba to your computer and use it in GitHub Desktop.
This file contains hidden or 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 gifAllTheThings(images) { | |
var div = document.getElementById('my-div'); | |
var index = 0; | |
var duration = 400; // duration of each image. 400 is arbitrary. change as you please | |
setInterval(function() { | |
var image = images[index % arr.length]; | |
div.style.backgroundImage = 'url(' + image + ')'; | |
index++; | |
}, duration); | |
} | |
function preloadImages(images, callback) { | |
var loaded = 0; | |
var load = function() { | |
loaded++; | |
if (loaded == images.length) { | |
callback(); | |
} | |
} | |
images.forEach(function(image) { | |
var i = new Image() | |
i.onload = load; | |
i.onerror = load; | |
i.src = image; | |
}); | |
} | |
var images = [url1, url2, url3, url4]; | |
preloadImages(images, function() { | |
gifAllTheThings(images); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment