Identify when a particular CSS animation for an element reaches completion

Within our Javascript implementation, we incorporate two CSS animation keyframes that enable the element to be displayed or hidden:

The first keyframe conceals the element as it is animated from top to bottom:

upperGuideText.classList.add("upperGuideAnimeHide"); // Activate Hide CSS KeyFrames

The second keyframe reveals the element by animating it from bottom to top:

upperGuideText.classList.add("upperGuideAnimeShow"); // Activate Show CSS KeyFrames

We seamlessly add and remove these keyframes via classes attached to the element.

Subsequently, we define a function called guideReveal to handle the show and hide animations.

Upon invoking the guideReveal function with a text input, there are two sequential steps:

Initially, the previous text is hidden (animated from top to bottom).

Subsequently, the new text passed to the function is added and animated (from bottom to top).

The implementation is straightforward; please review the following snippet:

function guideReveal(text){

// Hide the `upperGuideText` when executing the guideReveal function
upperGuideText.classList.add("upperGuideAnimeHide"); // Activate Hide CSS KeyFrames
upperGuideText.classList.remove("upperGuideAnimeShow"); // Activate Show CSS KeyFrames

 // Delay before displaying another `upperGuideText`
  setTimeout(function(){
  upperGuideText.innerHTML = `${text}`;

  upperGuideText.classList.add("upperGuideAnimeShow"); // Activate Show CSS KeyFrames
  upperGuideText.classList.remove("upperGuideAnimeHide"); // Activate Hide CSS KeyFrames

  //Detect the end of only the `upperGuideAnimeShow` animation! Currently not functional
  upperGuideText.addEventListener("webkitAnimationEnd", function(){
  console.log('Animation of guide ended..!')
  });

  }, 1000);
}

Although we can currently observe the animation's conclusion when the text appears or disappears using webkitAnimationEnd, I am solely interested in monitoring the end of the upperGuideAnimeShow animation...

How can we specifically Track the completion of the upperGuideAnimeShow animation?

Answer №1

If you are searching for a specific animationName attribute within the animationend event, you can do so by:

let element = document.querySelector('div')
element.addEventListener('animationend', function(event) {
   if (event.animationName === 'fade') {
     alert('Animation has ended')
   }
})
div {
   margin: 100px;
   width: 100px;
   height: 100px;
   background: yellowgreen;
   animation: fade 5s linear 0s 1,
              resize 2s linear 0s forwards;
   
}

@keyframes fade {
   from { opacity: 1 }
   to { opacity: 0 }
}

@keyframes resize {
   from { transform: scaleX(1) }
   to { transform: scaleX(2) }
}
<div></div>

In this instance, I have defined two different animations, but only checked for the 'fade' animation in the event handler.

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

``Trouble with React Dropdown menu option selection"

I am encountering challenges with implementing a dropdown menu list for my react app. The issue at hand is that I have an API where one of the keys (key3) has values separated by commas that I wish to display in my dropdown list. The structure of the API ...

When you hover over an image, both the image's opacity and the color of the title beneath it change. However, if the title expands to two lines, it messes up

I am currently working on a project where I have an image and a title displayed below the image. My goal is to change the color of the text when hovering over the image, as well as adjust the opacity of the image. Additionally, when hovering over the title ...

Implementing JQuery techniques to dynamically insert separate content into elements sharing the same class

I have utilized django to establish a model company featuring attributes like name and address. I aim to exhibit solely the names of all companies in the query-set. My desire is for jquery to unveil the corresponding address upon clicking the name (via aja ...

The following authentication error occurred: JWEDecryptionFailed - the decryption process has encountered a failure

[...nextauth]/route.js file import { User } from "@/lib/models"; import { connectToDb } from "@/lib/utils"; import NextAuth from "next-auth"; import GitHubProvider from "next-auth/providers/github"; export const aut ...

Unexpected patterns observed when utilizing parent/child routing files

I am working with a Node/Express backend that is implemented using TypeScript. Whenever I make changes to a file and save it, if I test the root route in Postman localhost:8000/, I receive the expected response. However, when I test localhost:8000/user af ...

Concealing a tab within a WordPress site

We have integrated a portfolio plugin with 4 categories, but we want to hide the All tab that is being displayed along with them. In order to achieve this, we need to include additional JavaScript in the header like so: (function($, undefined) { $(func ...

Activating style.display based on the date

Hello everyone! I'm new here so please bear with me if I make any mistakes. Can someone take a look and tell me what I'm doing wrong? I've created divs named "1st", "2nd", "3rd," etc., with the intention of setting up an advent calendar lay ...

Adjust the column count based on the screen size, Susy suggested

When using the Compass/Sass plugin Susy, you typically set the number of columns in the _base.scss file. I prefer to have 12 columns for a desktop view, but this may be too many columns for a mobile display. Is there a method to alter the number of column ...

Steps for regularly executing 'npm run build' on the server

My website doesn't require frequent content updates, as users don't always need the latest information. As a result, most pages are generated server-side and served as static pages. There will be occasional database updates that need to be visib ...

Tell me the permissions that a user possesses in discord.js

I need help creating a command using Discord.js that can display a user's permissions. For instance, the command could be $permissions @user and it should output something like: "User permissions within this guild: " I'm unsure if ...

Blank areas on a document

I've recently started learning HTML and CSS, and I've encountered a problem that has me stuck. HTML: <div class = "window"> <form> <input class="button" type="submit" value="Login" /> </form> ...

When attempting to access Firestore on a non-local server without using a virtual machine on AWS, the following error occurs: { Error: Module 'grpc' not found

My code runs successfully locally and connects to the same firestore. However, when I push it to my release server and hit the endpoint, I encounter this error: { Error: Cannot find module 'grpc' at Function.Module._resolveFilename (module.j ...

What steps can be taken to resolve the link prefetch warning in a Next.js application?

I am currently working on a Next.js application using version 13.4, and I've encountered a warning in the console: After preloading the resource at <URL>, it was not used within a few seconds of the window's load event. Please ensure that i ...

Enhancing Real-time Communication with NodeJS and NowJs Servers

Recently, I stumbled upon NodeJS and NowJS and I'm fascinated by the technology. My goal is to develop a Facebook-style instant commenting application where users can post comments that will instantly appear on the feed. After watching the screencast ...

What is the best way to verify if an XMLHttpRequest made to my public API originates from my own web application or from a third-party client in order to maintain priority?

Is there a method to determine on the API side whether a XMLHttpRequest is coming from my own web application (i.e. the JavaScript I developed) or from a third-party application? The issue appears to be that since the JavaScript runs on the client side an ...

Object responds to mouse click by rotating and moving through animation

Exploring the possibilities of three.js I'm attempting to create an animation (rotation/movement) for an object upon a mouse click event, however, I am not achieving the desired animation effects. Below is the code snippet that I have implemented. C ...

Determine the instance's name as a string in JavaScript

Currently, I am utilizing Three.js in combination with javascript. Upon running the following line of code: console.log(this.scene.children[1]) I receive the following output in the console within Chrome: https://i.stack.imgur.com/6LBPR.png Is there a w ...

When casting a ray from the inside, the raycast does not collide with the mesh

In my latest project, I've created a unique scene that involves placing my camera inside a sphere geometry. var mat = new THREE.MeshBasicMaterial({map: THREE.ImageUtils.loadTexture('0.jpg') , overdraw:true, color: 0xffffff, wireframe: fal ...

Align text vertically within a single flex container in Bootstrap

I am working on a simple card design that is divided into 3 sections: <div class="card" style="width: 350px; height: 250px;"> <div class="card-body"> <div class="d-flex flex-column" style="h ...

What could this error be in Chrome console: "Uncaught SyntaxError: Unexpected token ':'"

Why am I getting this error in the Chrome console: "Uncaught SyntaxError: Unexpected token ':'"? I have a JSON file located at the root of my application: <script src="levels.json"></script> Here is the content of my JSON file: { ...