Can someone help me with looping through CSS background images?
Why is the background not changing in this code example: http://codepen.io/anon/pen/dGKYaJ
const bg = $('.background').css('background-image');
let currentBackground = 0;
const backgrounds = [
bg.replace('url(','').replace(')',''),
'https://placehold.it/300x300&text=1',
'https://placehold.it/300x300&text=2'
];
function changeBackground() {
currentBackground++;
if(currentBackground > 2) currentBackground = 0;
$('.background').fadeOut(3000, () => {
$('.background').css({
'background-image': "url('" + backgrounds[currentBackground] + "')"
});
$('.background').fadeIn(3000);
});
setTimeout(changeBackground, 7000);
}
$(document).ready(() => {
setTimeout(changeBackground, 7000);
});
Any suggestions are welcome!
Thanks!