What is the best way to initiate animation on a child element once the parent element has reached 75% completion of its animation

Is there a way to use JavaScript to determine when an animation is 75% complete? I have many nested HTML elements that need to be animated with an animation delay property, where the nested animation should start when the parent element is 75% complete. I am familiar with the animationend event, but that's not quite what I'm looking for. You can view the example here.

HTML:

<div>
  <p>hello world</p>
</div>

Animation delay code:

div p {
  line-height: 200px;
  text-align: center;
  -webkit-animation-name: icon-bounce-in;
  -moz-animation-name: icon-bounce-in;
  -o-animation-name: icon-bounce-in;
  animation-name: icon-bounce-in;
  -webkit-animation-duration: .5s;
  -moz-animation-duration: .5s;
  -o-animation-duration: .5s;
  animation-duration: .5s;
  -webkit-animation-delay: .375s;
  -moz-animation-delay: .375s;
  -o-animation-delay: .375s;
  animation-delay: .375s;
}

In this visual example, I've manually delayed the execution of the p animation by 75%. How would I achieve this programmatically in JavaScript for a large number of elements (not necessarily by adding an animation delay to child elements, but by determining when the parent element is 75% into its animation and triggering the child element's animation)?

Answer №1

If you want to add a delay to stagger animations, consider using jQuery.

Check out this updated FIDDLE for reference

HTML

<div id="outer">
  <p id="inner">hello world</p>
</div>
<button id="clicker">click</button>

Javascript

$('#clicker').click(function() {
    var outerDuration = 500;
  $('#outer').css({top: '-50px', position: 'relative'})
             .animate({top: 0}, outerDuration);
  $('#inner').css({position: 'relative'}) // faced some issues with animating position
             .delay(outerDuration*.75) // adding the delay
             .animate({top: '-50px'}, 0, function() { 
                 $('#inner').animate({top: 0}, outerDuration)
             });
})

Answer №2

I recently discovered a clever little trick that I wanted to share with you all. Check out this JSFiddle for a demo.

Essentially, what I did was calculate the distance a div would travel during a transition, set up a watch using setInterval, and monitored its position every 10 milliseconds (this interval is customizable). When the div or its child p had traveled 75% of the total distance, I triggered an alert. You can customize this to call any function or perform any action at that point. Here's the code snippet:

var start = null; 
var flag = 1; // Direction of transition (use -1 for down to up)
var pos;
var distance = 200;

var interval = setInterval(function() {
   if (start == null) {
     start = $('div').position().top;
    }

   pos = $('div').position().top;
   console.log(pos, start, distance);
   
   if (pos >= (start + (0.75 * distance))) {
    alert("Animation is 75% complete");
    clearInterval(interval);
   }
}, 1);

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

Which CSS properties can I implement to ensure that the text remains legible regardless of the background behind it?

Looking ahead, I am displaying an issue where the colors are almost perfect, but when different percentages are applied, some or all of the text becomes obscured. My goal is to assign the font color based on the background difference. I vaguely remember s ...

Sending Parameters Using Higher Order Functions

Utilizing React and attempting to pass multiple arguments along with the 'event', I opted to implement a Higher Order function for this purpose. Unfortunately, it seems that the 'id' passed to the Higher Order function is not being rec ...

Using Jquery to toggle visibility and position of a button when clicked

I'm new to using Jquery and I've been able to create a button that shows a div and moves the button to the right when clicked. I have looked at similar questions about toggling visibility, but I also need to move the button back when it's cl ...

Determine the outcome with a PHP conditional statement for either "

I'm currently working on adding a function to a script where the user is prompted for a code, which I have successfully added. However, I am facing an issue in determining when to ask the user for the code and when not to. So, I believe I need to use ...

The null node is causing an error when trying to access the 'name' property

Defining Schema for Campground var campgroundSchema = new mongoose.Schema({ name: String, image: String, description: String, _id: String }); var Campground = mongoose.model("Campground", campgroundSchema); app.get("/campgrounds/:id" ...

Choose only one option from the dropdown menu at a time within the specified div

I attempted to utilize the "setSelected" option on my multiselect feature, but I noticed that it does not function with div elements (at least I could not make it work myself). I am endeavoring to create two synchronized multiselects using this example: b ...

Tips for keeping an image stationary when hovering over it

I'm in need of some assistance. Despite my best efforts, I have been unable to achieve the desired outcome. I am looking to create a scenario where an image remains in place even when the mouse hovers over it. Essentially, I want the image to stay vis ...

What benefits can be gained from utilizing the beforeEach function within Jest testing?

I am currently immersing myself in the world of Jest by following this informative guide. Can you explain the benefits of utilizing the beforeEach function in Jest? I have a particular interest in identifying action dispatches. I believe that two segments ...

Transform an array into a string with spaces in between each value by combining the elements

I have a complex select input on my website. By using jQuery, I can easily extract the selected values of all child option elements like this: $("#select").val(); This action returns an array as demonstrated below: ["Hello", "Test", "Multiple Words"] N ...

Unexpected behavior exhibited by DOM elements

I can't seem to figure out this issue. Every time I run this loop: for ( var i=0; i < 10; i++ ) { var $items = $(balls()); console.log($items); $container.imagesLoaded(function(){ $container.append( $items ).masonry( 'app ...

What is the identifier for the class that is currently active?

I am attempting to retrieve the id of the element that is currently active. While I have checked out this thread for guidance, I am still unable to achieve the desired outcome. <ul class="nav nav-pills nav-stacked navigation" id="configuration_sidebar_ ...

Gather and consolidate all files into a single file using Node.js / JavaScript

I currently have 3 JSON files located in my project/Folder file1.json { "id":"01", "name":"abc", "subject":[ "subject1":"Maths", "subject2":"Science" ...

Can Node.js fetch a PHP file using AJAX?

The Challenge: Greetings, I am facing an issue with my website that features a game created using node.js, gulp, and socket.io. The problem arises when I attempt to call a php file from node.js. While the file returns a success message (echo in the file) ...

Eliminate any unnecessary spaces in the text of a link following the breaking of a word

While working on a navigation menu, I noticed that in large screens the navigation looks fine but in laptop view, the words are breaking up and it's acceptable. However, I want to remove the extra spacing after the text. In the large screen: https:/ ...

Is it possible for JavaScript within an <iframe> to access the parent DOM when

Is it possible for javascript in an iframe from a different domain to modify the parent's DOM? I need my iframed script to add new html elements to the parent page - any suggestions? Edit: One potential solution is using "Fragment ID Messaging" to co ...

Track and maintain the active status of the clicked article using jQuery

I'm encountering an issue with jQuery where the parent ul li does not open as expected. Below is the structure of my HTML: <div> <ul> <li class="has-sub"> <a href="#">1</a> <ul> &l ...

jquery global variable not working as expected

I'm having trouble making some variables global outside the jQuery function. I've tried using 'var' to declare them first and then assigning values, but when I try to log() them at the end, I get undefined. var lat, lon; $.get('ip ...

Using a repeating background in CSS with overflow displayed at the top rather than the bottom

Does anyone know how to make the repeating background 'start' fixed at the bottom of the div and overflow on the top? I want it to be the opposite of the default behavior. Let me illustrate what I'm trying to achieve with a small example. I ...

Prevent the Sidebar from sticking to the bottom of the page

Here is the snippet of code where I encountered an issue within the else if statement: $( window ).on('load resize', function(){ var $sidebar = $(".sidebar_container"), $window = $(window), offset = $sideba ...

Issue with resizing causing layout glitches in jsPanel

Greetings and I hope you're enjoying your weekend! I just started working with jsPanel and I've encountered a small issue. Whenever I try to resize the panel, the layout glitches. The shadow disappears and the header becomes abnormally large. I& ...