Creating a Cross Fade Animation effect with the combination of CSS and JavaScript

I've been attempting to create a similar animation using html and css.

Below gif shows the desired outcome I am aiming for:

https://i.sstatic.net/YsNGy.gif

Although I have tried the following code, I have not been able to achieve the desired result. Can anyone provide guidance on how to implement this animation effectively?

.container {
  position: relative;
  width: 800px;
  /* Adjust this to match your image width */
  height: 400px;
  /* Adjust this to match your image height */
  overflow: hidden;
}

.overlay {
  position: absolute;
  top: 0;
  left: 100%;
  /* Start the overlay off-screen to the right */
  width: 100%;
  height: 100%;
  background: linear-gradient(to right, rgba(0, 0, 0, 0), rgba(0, 0, 0, 1));
  animation: slide 5s linear forwards;
}

@keyframes slide {
  0% {
    left: 100%;
    /* Start off-screen to the right */
  }
  100% {
    left: -100%;
    /* Slide to the left */
  }
}

img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: auto;
  /* Adjust as needed */
  object-fit: cover;
  /* Adjust as needed */
}
<div class="container">
  <div class="overlay"></div>
  <img src="test.png" alt="Input 1">
</div>

Answer №1

To achieve this effect, the recommended approach is to utilize the properties of background-size and background-position.

If you are looking for a handy tool to experiment with and understand what's happening behind the scenes, check out this interactive tool:

.container {
    width: 300px;
    height: 150px;
    border: 3px solid black;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(270deg, #eee909, #0969ee);
    background-size: 400% 400%;
    animation: slide 30s ease infinite;
}

.banner {
    font-weight: bold;
    color: white;
    background-color: gray;
    padding: 0.5em 1.5em;
    border: 1px solid black;
}

@keyframes slide {
    0%{background-position:0% 99%}
    50%{background-position:100% 2%}
    100%{background-position:0% 99%}
}
<div class="container">
    <div class="banner">some text</div>
</div>

Answer №2

After experimenting, I discovered a solution by expanding the .overlay box and applying the gradient only to the initial section. The revised CSS for your .overlay should appear as follows:

.overlay {
  position: absolute;
  top: 0;
  left: 100%;
  /* Start the overlay off-screen to the right */
  width: 200%; // Increase width by double
  height: 100%;
  background: linear-gradient(to right, rgba(0, 0, 0, 0), rgba(0, 0, 0, 1), rgba(0, 0, 0, 1)); // Add another layer of gradient to achieve desired effect
  animation: slide 5s linear forwards;
}

No further modifications are necessary.

This technique works because the animation shifts the gradient only by 100% to the left. By extending the overlay to twice its size, the gradient moves out of view while maintaining the dark background.

The final code snippet incorporating these changes is shown below:

.container {
  position: relative;
  width: 800px;
  /* Adjust according to image width */
  height: 400px;
  /* Adjust according to image height */
  overflow: hidden;
}

.overlay {
  position: absolute;
  top: 0;
  left: 100%;
  
  width: 200%;
  height: 100%;
  background: linear-gradient(to right, rgba(0, 0, 0, 0), rgba(0, 0, 0, 1), rgba(0,0,0,1));
  animation: slide 5s linear forwards;
}

@keyframes slide {
  0% {
    left: 100%;
    
  }
  100% {
    left: -100%;
    
  }
}

img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: auto;
  
  object-fit: cover;
  
}
<div class="container">
  <div class="overlay"></div>
  <img src="test.png" alt="Input 1">
</div>

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

What is the best way to access an Ajax response outside of its function using JQuery?

Just starting out with JQuery and wondering how to utilize the Ajax response ( HTTP GET ) outside of the function in my code snippet below: $.ajax ({ type: "GET", url: "/api", success: success, error: error ...

Should we consider the implementation of private methods in Javascript to be beneficial?

During a conversation with another developer, the topic of hacking into JavaScript private functions arose and whether it is a viable option. Two alternatives were discussed: Using a constructor and prototype containing all functions, where non-API meth ...

Feature for jotting down notes, creating a break between lines without any information present

I'm currently working on implementing a note-taking feature for my web application. However, I have encountered two issues: Firstly, I am struggling to identify line breaks within the text. While I can hardcode text with line breaks, when a user clic ...

The NetSuite https.post() method is throwing an error that reads "Encountered unexpected character while parsing value: S. Path '', line 0, position 0"

I'm currently facing an issue when trying to send the JSON data request below to a 3rd party system using the "N/https" modules https.post() method. Upon sending the request, I receive a Response Code of "200" along with the Error Message "Unexpected ...

Unable to get click function to work with multiple div elements

Using four div elements, I have implemented a feature where clicking on the first div causes all other div elements to change opacity. This is working correctly. However, I would like the opacity of the first div to remain unchanged when moving to another ...

Displaying array data without the need for a loop in Vue.js and Axios

I want to display data in my Vue.js 3 app without using a loop. Here is the response from my Axios API: In My Axios Api I got reponse: [{id: 2, name: "sub_title", value: "The Best Developer Team", created_at: null, updated_at: null},… ...

React checkbox displaying incorrect render

I'm currently working on developing a React component that acts as a tile representation. This particular component consists of a div containing a label and a checkbox. The issue I'm facing is that I can click anywhere on the component to trigg ...

How to Use the env() Variable in CSS Styling?

Operating on React with Material-UI, my web app features a bottom nav bar. For certain devices like the latest iPad, I must allocate an additional 8 pixels below it to account for the horizontal gray bar that substitutes for a home button: https://i.sstat ...

Default exports are not supported in TypeScript

I'm encountering issues with my Laravel + Vite + Vue 3 project. I followed the installation instructions in the documentation and everything works fine when the project is separated from Laravel and Vite. However, I'm facing a problem where TypeS ...

Creating a seamless rotation effect on an SVG shape at its center across all browsers, even Internet Explorer

Is there a way to make an SVG figure rotate around its center? I have been trying to calculate the rotation center and scale it based on the viewBox. It seems to work perfectly fine in Chrome, Firefox, and Safari, but I just can't seem to get it to wo ...

I am experiencing some issues with my AngularJS JavaScript code. The root scope is updating correctly, but the other two scopes are not working as expected. Can

My attempt at writing AngularJS JavaScript code seems to be malfunctioning. While the root scope updates properly, the other two scopes FirstCtrl and SecondCtrl do not update as expected when the root scope is updated. I would like all three scopes to upd ...

Align the button at the center of the carousel

While working on a personal HTML/CSS/Bootstrap 5 project as a self-taught learner, I have encountered some beginner doubts. My challenge is to ensure that the site remains responsive across various devices such as mobile and tablet. Specifically, I am stru ...

ng serve issue persists even after resolving vulnerabilities

Can anyone assist me in resolving why I am unable to start my project after fixing 3 high vulnerabilities? I ran npm audit to identify the vulnerabilities and then used npm install --save-dev @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_em ...

When viewed on a mobile device, the page defaults to displaying the NumPad instead of the Text Keyboard in Angular

The email field in my angular app is opening a Key Pad in mobile view and I'm not sure why. <form (ngSubmit)="onSubmit()" #emailForm="ngForm"> <div class="emailaddress" style="text-align:center;" *ngIf="!showEmail"& ...

Inquiring about browserify or webpack on a website using node - a few queries to consider

Seeking assistance with a website project I am currently working on. The project consists of 7 HTML documents, 3 stylesheets, 8 JavaScript files (including jQuery.min.js and some additional jQuery plugins), and various images. I am looking to bundle and mi ...

Content displayed above a slideshow of images in a stunning gallery on Squarespace platform

Currently working on a one-page landing page using Squarespace and running into some issues with creating a slideshow. You can check out my website here (the slideshow is located at the bottom of the page): Squarespace provides default options for text p ...

What is the best way to retrieve multiple image file names using JavaScript?

I attempted to use the code snippet below to retrieve the names of all the images I selected, however when I used 'alert', I only received one name even though I selected multiple. My goal is to have all the names of the selected photos saved in ...

Ways to create an oval background for my button

Can someone help me create a button with a unique shape similar to the Products button on stack overflow? Check out this image for reference I attempted using border radius, but it didn't give me the desired effect. I searched for other options but c ...

IE compatibility with CSS2 selectors

Is there a plugin, preferably jQuery, that can enable the use of CSS2 selectors like 'parent > child' and 'element:first-child' in my stylesheet for IE6, which doesn't seem to support them natively? ...

Tips for eliminating flicker upon the initial loading of a webpage caused by dynamic changes in CSS styles through JavaScript

Struggling with the load order of my CSS and JavaScript... Currently, I have a div with a blue background that is styled to appear sliced diagonally. However, upon initial page load (or CTRL+SHIFT+R), there's a brief moment where the slice effect doe ...