Finding a particular CSS3 Keyframe using JavaScript

Is there a way to detect when an animation reaches a specific keyframe, like 50% or 75%?

I attempted the following:

element.addEventListener("animationend", AnimationListener, false);

However, this only supports animationstart, animationiteration, and animationend events.

View example in action

Answer â„–1

Exploring the provided Fiddle example, the main goal is to determine when the #sun element's bottom property reaches a value of 100px. This can be achieved by utilizing getComputedStyle() to monitor the value, stopping the interval once it equals 100px, and then executing customized code:

var style=window.getComputedStyle(document.getElementById("sun")),
interval=setInterval(function(){
        if(parseInt(style.getPropertyValue("bottom"))===100){
            clearInterval(interval);
            console.log("50% reached");
        }
    },1);
#sun{
    animation:sunrise 1s ease;
    bottom:0;
    background:#ff0;
    border-radius:50%;
    height:50px;
    position:absolute;
    width:50px;
}
@keyframes sunrise{
    0%{
        bottom:0;
        left:0;
    }
    50%{
        bottom:100px;
    }
    100%{
        bottom:0;
        left:400px;
    }
}
<div id="sun"></div>

If you need to track multiple values, simply set up another interval. In this scenario, as per the example provided, when the animation hits 75%, the bottom property should ideally be at 50px. Due to potential variations across browsers, consider checking for values less than or equal to 50 instead:

var style=window.getComputedStyle(document.getElementById("sun")),
interval=setInterval(function(){
        if(parseInt(style.getPropertyValue("bottom"))===100){
            clearInterval(interval);
            console.log("50% reached");
            interval=setInterval(function(){
                if(parseInt(style.getPropertyValue("bottom"))<=50){
                    clearInterval(interval);
                    console.log("75% reached");
                }
            },1);
        }
    },1);
#sun{
    animation:sunrise 1s ease;
    bottom:0;
    background:#ff0;
    border-radius:50%;
    height:50px;
    position:absolute;
    width:50px;
}
@keyframes sunrise{
    0%{
        bottom:0;
        left:0;
    }
    50%{
        bottom:100px;
    }
    100%{
        bottom:0;
        left:400px;
    }
}
<div id="sun"></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 Library reference causes the anchor hover function to malfunction

Feeling frustrated after encountering an issue where the Bootstrap button class interferes with the hover function in standard CSS. Decided to create a custom solution by adding a grey box next to the button to display hover information instead of using t ...

Establish a connection to a designated route in Node.js

I have a specific path that I want to establish a socket connection to: http://localhost:3000/home/. Below is my index.js code: var express = require('express'); var app = express(); var http = require('http').Server(app); var path = r ...

Exploring CSS animations by utilizing the transform and color properties

I am currently working on a project that involves animating text (specifically "happy birthday to you") and a heart. The idea is for the heart to drop and hit each word one by one, turning them yellow upon impact. Once the last word is hit, the text shou ...

By utilizing the power of JSONP, data transfer limitations can be eliminated

Is it possible to remove the limitation on data sent using JSONP? Here's my code. I'm attempting to send 3000 characters (an image converted to base64 data) at a time to a service (serviceCall.ashx). Since my data is quite large, up to 30,000-40, ...

Can you explain the meanings of ?. and ?? in JavaScript?

Can anyone explain the significance of ?. and ?? in the following code snippet? history.replace(location.state?.redirectPath ?? '/home') ...

Issue with collecting data in Meteor Collection

After making some edits to another file and increasing the complexity of my application, a tracker function that was previously working fine is now experiencing issues. It is no longer returning any data for vrLoan fundingData This is a snippet of my c ...

How can I apply CSS properties to a div class by targeting another child div class?

When the div class dhx_list_item dhx_list_day_events_item is present, I need to apply the following CSS: .dhx_view .day_events .dhx_scroll_cont:first-of-type:before { //some code here } Please see the attachment for reference. ...

React hook, when not properly called, may result in an error known as

While working on a project with React hooks, I encountered the error message below. Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might ha ...

Preventing jQuery parser from turning arrays into objects

My JavaScript data file has the following structure: data = { items : [ {name: 'ABC'}, {name: 'CDF'} ] } After passing this data to $.ajax(type: 'POST', data: data), the converted data appears like this: it ...

Launch current screen inside a jquery dialog box

When attempting to open an existing aspx page within a jQuery dialog, I noticed that the CSS of the parent screen is being overridden by the dialog box. Is there a way to prevent this without making changes to the existing screen? If there are any altern ...

"An ExceptionInInitializer error occurred - Can you provide more details,

An Issue Has Arisen: Exception in thread "main" java.lang.ExceptionInInitializerError at com.PoseidonTechnologies.caveworld.level.WoRe.<init>(WoRe.java:46) at com.PoseidonTechnologies.caveworld.CaveWorld.start(CaveWorld.java:80) at com.P ...

"Exploring Firebase features, what is the process for generating a query using UID as a reference point

I recently developed a function that assigns a UID to the database along with an item's state: changestate(item) { var postData = { state: "listed", }; var user = firebase.auth().currentUser; var uid = user.uid; var updat ...

Click and keystroke fusion

Greetings everyone, I've been attempting to integrate both click and keypress functionalities into my function, but unfortunately, nothing I've attempted so far has yielded any success. Here's the code snippet: function victoryMessage() { ...

Troubleshooting Django Python: Why can't I retrieve a JS object sent via AJAX in my Python QueryDict?

As I work on my Django project, I've set up a system for data exchange between the server and client using javascript and python with AJAX. To make things easier, I created a config object in JS to store important parameters that both the server and J ...

Is it possible to enhance controllers in Sails.js through extension methods?

There are times when I need to execute a certain action on every page of my web application or make a specific method available to all controllers. Previously, in object-oriented MVC frameworks, I would have my controllers extend a base controller and de ...

A comprehensive guide on making an AJAX call to a self-hosted servlet

I created a servlet in Eclipse IDE for Java EE that posts data as an XML page and hosted it on a Tomcat server. The servlet can be accessed at http://localhost:8080/Checkers/CheckersServlet. When I open this URL in Firefox, the XML loads correctly. Now, I& ...

Firebase Error: Trying to access properties of null object (indexOf)

Whenever I try to console.log(docSnap), a Firebase error shows up as seen in the image below. Despite attempting various solutions, none have proved effective. https://i.sstatic.net/9R4vE.png useEffect(() => { if (folderId === null) { ...

Icon on Google Maps replaced by gray box

Incorporating the Google Maps API into my Django project to showcase the current users location is a new endeavor for me. The map loads successfully, however, there's an issue with a grey box appearing instead of the map marker (specifically hovering ...

Issue with PHP Ajax Image Upload

I am currently working on setting up an Ajax image upload feature. Unfortunately, I am encountering issues and unable to identify the root cause. <script> $(document).ready(function() { $("#uploadBTN").click(function(event) { ...

Changing the appearance of a website by switching CSS stylesheets according to the size of the browser

Trying to build two versions of my website: one for mobile devices or small browsing windows, and another for large browser windows. Looking to link HTML to different style sheets based on browser size. Current attempt in code only addresses total screen ...