As part of my learning, I have manage to set the background image of my page and change the image every 10 seconds.
However, the change between images is harsh with a white background shown before the next image. Is there anyway I can fade each image in/out? All help will be appreciated.
* { margin: 0; padding: 0; }
html {
background: url(/cover1.webp) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
</style>
<div class="hero is-fullheight">
</div>
<script>
function changeBg(){
const images = [
'url("/cover1.webp")', 'url("/cover2.webp")', 'url("/cover3.webp")', 'url("/cover4.webp")',
'url("/cover5.webp")', 'url("/cover6.webp")', 'url("/cover7.webp")', 'url("/cover8.webp")',
'url("/cover9.webp")', 'url("/covera.webp")', 'url("/coverb.webp")', 'url("/coverc.webp")',
]
const selector = document.querySelector('html');
const bg = images[Math.floor(Math.random() * images.length)];
selector.style.backgroundImage = bg;
}
setInterval(changeBg, 10000)
</script>
2
Answers
Just changing the html style tag produced the effect I wanted:
You can add animation properties to your images. For example if you have images with class "image".