What could be the reason that my image animation code is not functioning properly?

I've been trying to create an animation using CSS and HTML, but for some reason, my animation just isn't working properly.

Take a look at this example of what I'm hoping to achieve (you'll need to scroll down) and compare it with the code I've used. Unfortunately, the black screen keeps glitching back. Any suggestions or help would be greatly appreciated!

html,
body {
  height: 100vh;
}

body {
  margin: 0;
  width: 100%;
}

.image {
  animation: change 0.5s ease-out;
  height: 70vw;
}

.img {
  background-image: url("https://www.abroprojectafbouw.nl/wp-content/uploads/moddit-fly-images/468/foto-15-scaled-770x500-c.jpg");
  background-size: cover;
  position: relative;
  width: 100%;
  height: 70vw;
}

.overlay {
  position: absolute;
  width: 100%;
  height: 70vw;
  background-color: #000;
  top: 0;
  right: 0;
  animation: slide 0.5s ease-out;
  animation-delay: 0.6s;
}

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

@keyframes change {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}
<div class="image">
  <div class="img">
    <div class="overlay"></div>
  </div>
</div>

Answer №1

Make sure to include overflow: hidden in the .img class and add forwards for the slide animation.

html,
body {
  height: 100vh;
}

body {
  margin: 0;
  width: 100%;
}

.image {
  animation: change 0.5s ease-out;
  height: 70vw;
}

.img {
  background-image: url("https://www.abroprojectafbouw.nl/wp-content/uploads/moddit-fly-images/468/foto-15-scaled-770x500-c.jpg");
  background-size: cover;
  position: relative;
  width: 100%;
  height: 70vw;
  overflow: hidden;
}

.overlay {
  position: absolute;
  width: 100%;
  height: 70vw;
  background-color: #000;
  top: 0;
  right: 0;
  animation: slide 0.5s ease-out forwards;
  animation-delay: 0.6s;
}

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

@keyframes change {
  0% {
    width: 0%;
  }
  100% {
    width: 100%;
  }
}
<div class="image">
  <div class="img">
    <div class="overlay"></div>
  </div>
</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

The Bootstrap row has surpassed the maximum height of the container-fluid

I've encountered a problem while working with the bootstrap grid system (Version v4.5.2). I have set a max-height of 50vh on a container-fluid, which is working fine. However, when I add a row with 2 columns to the container, the images within the row ...

Start the video on repeat from the chosen time frame

I am working on incorporating a video file into my design that is 2 minutes in length. I want the video to play from the beginning initially and then loop from 1.00 sec to 2.00 sec. I have attempted various methods, but there seems to be a noticeable jump ...

Which component from the Bootstrap library would be best suited for my needs?

I am looking to create a basic 6-page website. Here is the code for my main page: <html> <style> .ali{ width:170px; text-align:center; } footer{ background:#333; color:#eee; font-size:11px; padding-top: 25px; p ...

What could be causing the issue with HTML not being printed upon button click in ReactJS?

My goal is to display the word "Hello" on the screen when the add button is clicked. However, I am encountering an issue where it is not showing up. Any insights or solutions would be greatly appreciated! import React, { Component } from 'react'; ...

Unable to switch the text option

[Fiddle] I'm currently working on a project where I want pairs of buttons to toggle text by matching data attributes. While I can successfully change the text from "Add" to "Remove" on click, I am facing an issue with toggling it back to "Add" on the ...

Modify the parent's style in CSS based on the presence of certain specified children

I'm working with an HTML element that can contain a child with the ID 'slideshow' <div id='content'> <div id='slideshow'> </div> </div> Alternatively, it could have a different child like t ...

What is the method for altering the value of a class within another class?

Check out this piece of code: <div id="one"> <div id="two"> <div class="cell_pad"> </div> </div> </div> How can you instruct CSS to modify the value of class "cell_pad" within the container of id="one"? Th ...

Retrieving data from a <span> element within a webpage created using ReactJS

I am completely new to the world of web design and development, so there may be some mistakes in my request. Essentially, I have a webpage that contains numerous span tags like the following example: These span tags are part of a significantly large DOM t ...

Is it possible to use one HTML button to submit a token and create a charge in Stripe?

<!-- Payment Form --> <form action="/payment" method="post" id="payment-form"> <div class="form-row"> <label for="card-element"> Ent ...

Displaying every nth image in a new row of a CSS grid

I have a react component and I want to arrange 3 photos inline in a row with their names below each photo, and then continue in new rows for the next set of 3 elements and so on... I attempted to use :nth-child(3n+1) but encountered some issues... const P ...

Integrating JSON back into the system

As I work with Axios to retrieve information from an API, I am able to successfully connect and receive an object with the necessary data. However, I am struggling to display this JSON data in my HTML code. Here is the code snippet I am using: <body&g ...

Overruled fashion

Here is the HTML code from my custom page: <div class="notes quotes-notification member-savings f-left"> <h2>My Notifications</h2> <ul> <li><b>I need to make some changes in the HTML, different from other pages you have ...

The mobile version of my website's home page is not displaying certain elements of the featured image

I'm in the process of launching a fresh new blog with Flask as the backend. Currently, I'm using a Bootstrap template and have set an image as the featured image. While it displays properly on computers, tablets crop it out. You can check out the ...

Extract a CSS property and store it in a variable using JQuery

Is there a way to extract a CSS value from a specific class on a webpage and then use it for styling other elements in different ways? I created an example on jsfiddle where I want to retrieve the background color from .box and save it to a variable nam ...

Modify the color of CSS for all elements except those contained within a specific parent using jQuery

I have a color picker that triggers the following jQuery script: //event.color.toHex() = hex color code $('#iframe-screen').contents().find('body a').css('color', event.color.toHex()); This script will change the color of al ...

Display selected divs with dropdown box but show all divs when no option is chosen

I came across a helpful code on this website while working on my own website. After customizing it to my liking, I encountered some issues. My main goal is to have all divs displayed when no value is selected from the dropdown box. Below is the code that I ...

How can I create a responsive design for my div elements?

I am facing an issue with responsiveness in my div container. Inside the square-shaped frame, I have a h2 tag that does not adjust properly when viewed on different devices such as mobile and browsers. Despite setting up media queries to make it responsive ...

Troubleshooting: Why isn't my JavaScript Array being sent to PHP file through

I am encountering an issue with transferring data from a JavaScript array, obtained from checkboxes in an HTML form, to a PHP page using AJAX. Despite successfully logging the data into the console, it appears to be lost when reaching the PHP page. My obje ...

Disabling Scrolling in AngularJS Material Tab Components

I'm experimenting with AngularJS Material components and struggling with creating tabs. Every time I add a tab, the content inside the child md-content element automatically gets a fixed height with a vertical scrollbar instead of adjusting its heigh ...

Having trouble with submitting my information on Selenium's login form

Having trouble validating my credentials on a login form website using Selenium Python Html source code <div id="login_button_container"> <span id="login_quick_clock_container"> <a class="button1 clock_out" o ...