Tips for eliminating the white flash while transitioning background images with animation?

I am facing an issue with my code where the background image of the site transitions between 5 different images, but every time an animation completes and the next one starts, there's a white flash. I'm not sure how to resolve it or what I might have done wrong. Can someone help me troubleshoot this problem?

@keyframes backswitch {
  0% {
    background-image: url("background.jpg");
  }
  20% {
    background-image: url("background_2.jpg");
  }
  40% {
    background-image: url("background_3.jpg");
  }
  60% {
    background-image: url("background_4.jpg");
  }
  80% {
    background-image: url("background_5.jpg");
  }
  100% {
    background-image: url("background_6.jpg");
  }
}

body {
  /*Adjusting images and animation*/
  background-repeat: no-repeat;
  max-width: 100%;
  max-height: 100%;
  background-size: cover;
  animation-name: backswitch;
  animation-duration: 60s;
  animation-iteration-count: infinite;
}
<!DOCTYPE html>
<html>

<body>
</body>

</html>

Answer №1

To effectively load your images in advance, you need to preload them:

@keyframes backswitch {
  0% {background-image: url("https://dummyimage.com/300/ccc/fff.png");}
  20% {background-image: url("https://dummyimage.com/300/3f5/fff.png");}
  40% {background-image: url("https://dummyimage.com/300/71c/fff.png");}
  60% {background-image: url("https://dummyimage.com/300/228/fff.png");}
  80% {background-image: url("https://dummyimage.com/300/c11/fff.png");}
  100% {background-image: url("https://dummyimage.com/300/544/fff.png");}
}
body {
  /*Adjusting images and animation*/
  background-repeat: no-repeat;
  max-width: 100%;
  max-height: 100%;
  background-size: cover;
  animation-name: backswitch;
  animation-duration: 60s;
  animation-iteration-count: infinite;
}

div.preload-images {
  background: url("https://dummyimage.com/300/ccc/fff.png") no-repeat -9999px -9999px;
  background: url("https://dummyimage.com/300/ccc/fff.png") no-repeat -9999px -9999px,
    url("https://dummyimage.com/300/3f5/fff.png") no-repeat -9999px -9999px,
    url("https://dummyimage.com/300/71c/fff.png") no-repeat -9999px -9999px,
    url("https://dummyimage.com/300/228/fff.png") no-repeat -9999px -9999px,
    url("https://dummyimage.com/300/c11/fff.png") no-repeat -9999px -9999px,
    url("https://dummyimage.com/300/544/fff.png") no-repeat -9999px -9999px;
}
<body>
  <div class="preload-images"></div>
</body>

Answer №2

If you set the background-image property for the body, you can preload all images and eliminate this issue.

body {
  background-image: url("image1.jpg"), url("image2.jpg"), url("image3.jpg"), url("image4.jpg"), url("image5.jpg"), url("image6.jpg");
  background-repeat: no-repeat;
  max-width: 100%;
  max-height: 100%;
  background-size: cover;
  animation-name: backswitch;
  animation-duration: 60s;
  animation-iteration-count: infinite;
}

@keyframes backswitch {
  0% {
    background-image: url("image1.jpg");
  }
  20% {
    background-image: url("image2.jpg");
  }
  40% {
    background-image: url("image3.jpg");
  }
  60% {
    background-image: url("image4.jpg");
  }
  80% {
    background-image: url("image5.jpg");
  }
  100% {
    background-image: url("image6.jpg");
  }
}

Answer №3

After investigating the performance of regular images (not background images), I found that they do not experience the same problem, at least in Firefox and Chrome browsers. To resolve this issue, you can place your top layer content within a relative div with z-index 1, then insert an image into an absolute div with z-index 0. Updating your CSS shouldn't be too time-consuming and should help everything align properly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to making a dropdown menu that pulls information from a text box and displays the location within the text

I'm currently working on a unique dropdown feature that functions like a regular text field, displaying the text position. To achieve this, I've constructed a hidden dropdown menu positioned beneath the text field. My goal is to develop a jQuery ...

Tips on utilizing browser scroll for horizontal overflow of internal div?

I'm working on creating a dynamic page with a tree-like structure that easily exceeds the width of the browser window. My goal is to enable horizontal scrolling for the entire page using the browser's scrollbar, without needing a separate scrollb ...

Reordering the stacking order of Grid Items in material-ui

I'm facing an issue with the stacking order of grid items when the browser shrinks. On a regular desktop screen (lg): --------------------------------------------- | col 1 | col 2 | col 3 | --------------------------------------- ...

Achieve maximum height with background images

Having trouble with responsiveness on my box containing a background image. I am aiming for the box to adjust its height based on the background image, as I have set the background-size attribute to auto. html: <div class="index_boxs" id="discover_win ...

What could be causing my tab code to not function flawlessly?

I am attempting to implement a tab concept on my website. For example, the tab names are Monday...Tue.. up to Sunday. Each tab contains an image file based on the days (size 460*620). When I run my page, it shows all images, but what I need is for the imag ...

fixed divs that remain in place while scrolling

In the past, I have implemented a method similar to this: However, I am not a fan of the "flicker" effect that occurs when the page is scrolled and the div needs to move along with it. Recently, I have come across websites where certain elements (such as ...

Guide to setting up a node.js server that serves HTML files with CSS styling

I am currently working on setting up a simple node.js server for my HTML using http and express. Everything is functioning properly, but the CSS is not displaying as expected. Here is the code snippet for my server setup: const express = require("express ...

Having trouble with the auto suggest feature in PyCharm with Material Design Lite?

Struggling with setting up a fresh Django project in PyCharm and encountering issues with HTML code autocompletion. I've integrated Material Design Lite as the front-end framework, storing the files in a static folder within the project. I attempted ...

Integrate blogger into my website with automatic height adjustment and scrolling functionality

Check out my website here. I've integrated a blogger component into it. Can anyone provide guidance on creating parallel scroll and automatically resizing sections based on the blog's height? Thanks, M ...

Generate a custom map by utilizing JavaScript and HTML with data sourced from a database

Looking for guidance on creating a process map similar to this one using javascript, html, or java with data from a database. Any tips or suggestions would be appreciated. https://i.sstatic.net/tYbjJ.jpg Thank you in advance. ...

Disappearance of external page upon refreshing - jquery mobile

I'm in the process of developing a jQuery mobile application that loads external pages into a specified div when a link is clicked. These externally loaded pages contain links to additional pages. However, I've noticed that when I click on these ...

Troubleshooting Symfony2 and Assetic: Why are my CSS-linked images not loading?

I've encountered an issue with my CSS sprite sheet that my CSS file seems unable to locate the image. Despite following the provided solution here, I'm still facing the same problem. The directory structure of my bundle is as follows: src/ v ...

Create dynamic automatic titles in HTML with JavaScript

Below is the code snippet to add an image with a link to the home-page and an h1 with the document name (if there isn't one already). This HTML code includes a JavaScript file reference in the <head> section and uses a <h1> tag for the ti ...

Use Jquery to swap out text without the need for a separate update button

Looking to replace "Gastos de envío: " with "Shipping costs" on the English page. I currently have this jQuery code (which is working), but it only replaces the text the first time you visit the page. If you update the shipping costs, the jquery does not ...

Is Intellisense within HTML not available in SvelteKit when using TypeScript?

Having trouble with intellisense inside HTML for a simple page component. Also, renaming properties breaks the code instead of updating references. Typescript version: 4.8.4 Below is the code snippet: <script lang="ts"> import type { Bl ...

What is causing the variations in line heights on this webpage?

I have recently created a test webpage, and I noticed that despite using the same CSS style, the line heights are different. This discrepancy is more prominent when viewing the page in Chrome and highlighting the usage of the .instruction class. It appears ...

Looking to extract the hidden value from an HTML input field using Selenium

Unable to retrieve the value of a hidden element in HTML, even though it is displaying. Seeking assistance in accessing the value despite the type being hidden. <input type="HIDDEN" label_value="Sub Reference" title="Sub Reference" id="ACC_NO" dbt="BK_ ...

Error Message: Undefined Constructor for Firebase Google Authentication

Hey there! I've been working on integrating Firebase google authentication into my project. Unfortunately, I encountered an error while testing it out. Here's the error message that appeared in the console: Uncaught (in promise) TypeError: Cannot ...

Gradually alter the content within the div while transitioning the background color

I'm working with a div that has the following CSS properties: #results { background: #dddada; color: 000000; cursor: pointer; } #results:hover { background: #3d91b8; color: #ffffff; } How can I smoothly change just the text inside this div, u ...

Tips on creating Twitter Bootstrap tooltips with multiple lines:

I have been using the function below to generate text for Bootstrap's tooltip plugin. Why is it that multiline tooltips only work with <br> and not \n? I would prefer to avoid having any HTML in my links' title attributes. Current Sol ...