background.js 944 B

12345678910111213141516171819202122232425262728293031323334353637
  1. const images = [
  2. 'images/bg1.png',
  3. 'images/bg2.png',
  4. 'images/bg3.png'
  5. ];
  6. let index = 0;
  7. // Preload the images
  8. const preloadImages = () => {
  9. images.forEach(src => {
  10. const img = new Image();
  11. img.src = src;
  12. });
  13. };
  14. preloadImages();
  15. const bg = document.getElementById('background');
  16. // Initially set background image without transition (to avoid flicker)
  17. bg.style.backgroundImage = `url("${images[index]}")`;
  18. bg.style.opacity = 1; // Ensure it's visible immediately
  19. // Change background image every 10 seconds with fade effect
  20. setInterval(() => {
  21. index = (index + 1) % images.length; // Cycle through images
  22. // Fade out first
  23. bg.style.opacity = 0;
  24. // After fade-out, switch image and fade-in
  25. setTimeout(() => {
  26. bg.style.backgroundImage = `url("${images[index]}")`;
  27. bg.style.opacity = 1;
  28. }, 1000); // Wait 1 second before applying the new background
  29. }, 10000); // 10-second interval for image change