background.js 982 B

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