I am trying to make the background image of my website to change every certain quantity of seconds, I have tried to do this with this:
var images = new Array(
'Space.gif',
'Windows.gif',
);
var slider = setInterval(function() {
document.getElementsByClassName('bg-img')[0].setAttribute('style', 'background-image: url("'+images[0]+'")');
images.splice(images.length, 0, images[0]);
images.splice(0, 1);
}, 10000);
body {
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
background-size: 100%;
}
<div class="bg-img" style="background-image: url('space.gif'),background-image: url('windows.gif');">
<div class="overlay"></div>
</div>
And I have also tried with this code of JavaScript:
function changeBg(){
const images = [
'url("Space.gif")',
'url("Windows.gif")',
]
const selection = document.querySelector('selection')
const bg = images[Math.floor(Math.random()* images.length)];
selection.style.backgroundImage = bg;
}
setInterval(changeBg, 1000000)
But nothing works :(