Retrieving fresh CSS data from earlier animated elements within a Greensock timeline

In my code, I am using a TimelineLite() to perform a series of sequential tweens with .to(). What I want to achieve is accessing the output value from one of the early tweens in order to use it for constructing later tweens.

Is there any way to retrieve the values of elements that have completed their animation in a timeline?

UPDATE:

The example I initially shared was quite basic. I have now revised it to better illustrate the issue I am facing.

The dimensions of colored divs rely on the viewport size, and the content within them adjusts accordingly. When you click on one, an animation starts where the clicked div expands to fill the entire viewport, while hiding the others. As a result of this resizing, the text reflows to accommodate the new space.

Subsequently, the previously off-page div.status moves up to be positioned below the selected colored div. Unfortunately, the height used to calculate its new `top` value still reflects the height of the selected colored div before the animation and content reflow take place.

var timeline = new TimelineLite();

$('.clickable').click(function(){
  var $selected = $(this);
  var $notSelected = $('.clickable').not($(this));
  $selected.addClass('selected');
  $notSelected.addClass('not-selected');
  
  timeline
  .add("optionSelected")
  .to(
    $selected,
    0.5,
    { "width":"96%" },
    "optionSelected"
  )
  .to(
    $notSelected,
    0.5,
    {
      "width":"0%",
      "padding":"0"
    },
    "optionSelected"
  )
  .to(
    $(".status"),
    0.5,
    {
      "top":$('.selected').height()
    }
  )
})
body{
  position:relative;
  height:100vh;
  overflow:hidden;
  background-color:cornsilk;
}
section{
  background-color:#ddd;
  display:flex;
  align-items: flex-start;
}
div{
  width: 30%;
  padding:2%;
  color: #FFF;
  display:inline-block;
}
.clickable{cursor:pointer;}
.status{
  width:96%;
  position:absolute;
  top:100%;
  left:0;
  background-color:#FFF;
  color:black;
  border:1px solid black;
}
.blue{background-color:blue;}
.green{background-color:green;}
.purple{background-color:purple;}
.not-selected{ 
  white-space: nowrap;
  overflow:hidden;
}
<body>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.19.0/TweenMax.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <section>
    <div class="blue clickable"><strong>Clickable</strong> - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Dignissim tortor, sit amet vulputate augue lectus vel felis. Cras ac ex vel ligula porta laoreet. 
    </div>
    <div class="green clickable"><strong>Clickable</strong> - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ultrices, est ut sollicitudin gravida, velit erat dignissim tortor, sit amet vulputate augue lectus vel felis. Cras ac ex vel ligula porta laoreet. Mauris lorem tellus, convallis ac tincidunt eu, efficitur consequat turpis.</div>
    <div class="purple clickable"><strong>Clickable</strong> - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ultrices, est ut sollicitudin gravida, velit erat dignissim tortor, sit amet vulputate augue lectus vel felis. Cras ac ex vel ligula porta laoreet.</div>

    <div class="status">Status</div>
  </section>
</body>

Answer №1

Consider using a function to return the desired height as a callback. This way, it will wait until after the animation of the .selected element has reached the specified to() point in the chain.

Remember that height() does not account for padding or margins, which may cause overlap with the .status element. Instead, try using .outerHeight().

Here is a sample working code:

http://example.com/code-sample

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

populating dropdown menu with data from database using jquery and ajax

Currently, I am utilizing jQuery to dynamically add a div element for select options. var div_secretary = div_secretary + '<div>Secretary:'; var div_secretary = div_secretary + '<select style="margin-left:5px;" id="select_reviewer_ ...

Differences between throwing errors, returning error objects, and using callbacks in Javascript

Currently, I am developing an assembler and simulator for a simplistic assembly language that my computer science students use during their classes. The project is being written in JavaScript with the intention of creating a user-friendly interface in the ...

Guide on navigating an array of objects using the provided keys as a starting point in Javascript/Typescript

Assuming I have an array of objects structured like this: const events: Array<{year: number, month: number, date: number}> = [ {year: 2020, month: 10, date: 13}, {year: 2021: month: 3, date: 12}, {year: 2021: month: 9, date: 6}, {year: 2021: mont ...

Is there a way to access the content of a drop-down menu in JavaScript besides using ".value"?

Trying to create a table with rows that change dynamically but columns that are fixed. There's a drop-down menu whose content is based on an xml file. When I use .value to access the current content of my drop-down menu, it works fine in Firefox but n ...

Step-by-step guide on developing an AngularJs provider using TypeScript

As I've developed a Directive that incorporates various Css classes, it would greatly enhance its flexibility if the Css classes could be configured at Application start within the config section. I believe utilizing a provider is the appropriate appr ...

Transforming control of execution seamlessly between two interactive functions in nodejs

Two functions are used to take input from the CLI using process.stdin. The issue arises when one function is done taking input, as a similar function is called. However, while the control shifts to the second function, the first function continues executin ...

Is it possible to utilize the router.query feature in a function within Next.js?

Running into a problem with my Next.js page. I'm attempting to utilize the request params in a function, but it keeps coming up as undefined. I've exhausted all my troubleshooting options. I already know that there's no need to include id i ...

Guide on dynamically changing the color of polymer 1.0 paper-button from an array of colors

I have an array called buttonColors that contains a set of colors in hex format. I am looking to create paper-buttons, each with the color from the buttonColors array. How can I achieve this in Polymer 1.0? <template is="dom-repeat" items="{{buttonCo ...

Adjusting the Aspect Ratio of an Embedded YouTube Video

<!DOCTYPE HTML> <head> <style> body { overflow: hidden; margin:0; } </style> </head> <body> <iframe id="video" src="https://www.youtube.com/embed/U4c9bBeUe4M?modestbranding=1&sh ...

Extract data from JSON object

My JSON object is called idAndNames.json; [ { "id":"1", "name":"name1"}, { "id":"2", "name":"name2"}, { "id":"3", "name":"name3"} ] I'm looking to filter it by ID and name function applyFilter(id, valueItem) { return id <= valueIte ...

Pull information from a database using PHP and MySQL to populate a dynamic AJAX dropdown menu with the

I have successfully implemented a dynamic multiple country dropdown using JQuery/PHP/MySql. Each user's country/state/town data is stored in the MySql Database under the table named (usertable) as shown below: id | country | state | town | 1 | ...

Having trouble obtaining outcomes from Cloud Code in Parse

After struggling with this issue for quite some time, I have reached a point where I feel the need to seek help. I am working on a cloud code function that is supposed to retrieve a list of categories along with their respective products. Each category con ...

What is preventing me from being able to import a React component from a file?

I've double-checked my code and everything seems correct, but when I view it on the port, only the app.js file is displayed. import React from 'react'; import ImgSlider from './ImgSlider'; import './App.css'; function ...

Struggling to make the controller karma test pass

I am currently working on developing a "To Do list" using angular js. My goal is to allow users to click on a checkbox to mark tasks as completed after they have been finished. While the functionality works fine in the index.html file, I am struggling to p ...

The AJAX status is now 0, with a ready state of 4

Struggling to execute an AJAX call (using only JavaScript) to store a user in the database. The JavaScript file I am working with includes this code: var url = "interfata_db.php"; xmlhttp.onreadystatechange = function(){ alert('ready state &apos ...

Using PrimeNG checkboxes to bind objects in a datatable

PrimeFaces Checkbox In the code snippet below, my goal is to add objects to an array named selectedComponents in a model-driven form when checkboxes are checked. The object type of item1 is CampaignProductModel, which belongs to an array called selectedC ...

Using jQuery to retrieve values from clicked buttons

I'm trying to retrieve the values of a jQuery button upon form submission, but my current setup is not working. Specifically, I need to extract the value of data-url. Below is the code snippet I am using: $("#addAgency").submit(function(event) { ...

Adjustable div heights for text within inline elements

How can I make two inline-table divs (content and sidebar) have the same height regardless of their content? I've experimented with various attributes, but nothing seems to be working. You can view my website here. ...

Using Jquery 1.5 to send Ajax requests with GET data even when using the POST method

(Apologies for any errors in my English, it is not my native language) I have a project that utilizes codeigniter+JqueryUI. I am considering upgrading the JQuery version to 1.5 mainly because I heavily rely on ajax calls, and any speed improvements would b ...

utilizing an ajax request to clear the contents of the div

When I click on Link1 Button, I want to use ajax to empty the contents in the products_list div <button type="w3-button">Link1</button> I need help with creating an ajax call that will clear the products in the product_list when the link1 but ...