Determining the ultimate height of an element as it transitions in size using CSS

In situations where a CSS transition is ongoing, jQuery will return the current height of an element when its dimensions are requested. However, this may not always be desired. There are many cases where it's necessary to retrieve the final dimensions that the element will have after the transition has completed but while the transition is still in progress.

Is there a reliable way to obtain the dimensions of an element during a transition?

$(document).ready(function() {
  $('#one').on('click', function() {
    $(this).addClass('trans');
    $('#output').text($(this).height());
    var self = $(this);
    setTimeout(function() {
      $('#output').text(self.height());
    }, 200);
  });
});
#output {}

#one {
  width: 200px;
  height: 200px;
  background: red;
  transition: all 1s linear;
}

#one.trans {
  height: 600px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div id="output">...</div>
<div id="one"></div>

Check out this codepen example

Answer №1

When utilizing the setTimeout function, one must often resort to utilizing a somewhat arbitrary value. A more efficient alternative is to utilize the requestAnimationFrame function, which ensures that the code executes after the transition has completed.

In many cases, it is necessary to wait until the second animation frame before executing code. This can be accomplished with the following nested requestAnimationFrame functions:

requestAnimationFrame(function() {
    requestAnimationFrame(function() {
        $('#output').text(self.height());
    });
});

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

How can I transfer an array from a servlet to HTML using jQuery?

As I parse an uploaded XML file with the dom, the generated string combines the hostname and osname values separated by a , delimiter. The variable s holds this string which is then sent back to HTML using the response.getWriter object obj. However, instea ...

The V-if condition is specifically tailored for a single line or link

I am attempting to conceal a link ("myprofile") beneath an image in VUE.JS when the if-statement is not met. However, when I insert the v-if inside the vue-router or div tag, the entire image/div-tag becomes hidden. How can I implement an if-statement that ...

Can you please explain the concept of straightforward UI routes to me?

I recently discovered that ui-routes are more powerful and advantageous than ngRoutes. In an effort to learn more about the ui-routing feature, I started studying from http://www.ng-newsletter.com/posts/angular-ui-router.html. However, I found it challengi ...

Why are my menu and title shifting as I adjust the page size?

I'm having trouble finalizing my menu and headings. Whenever I resize the browser window, they shift to the right instead of staying centered as I want them to. I would really appreciate any help with this. Below is the HTML and CSS code. var slide ...

Using regex to pick out every instance of zero that comes before a numerical value

I am currently utilizing PowerRename, a tool that allows for regex usage to select portions of filenames for replacement. My goal is to tidy up some filenames by removing the preceding zeroes before a number in the file name (replacing them with nothing). ...

Utilizing a combination of a `for` loop and `setInterval

I've been encountering an issue for the past 3-4 hours and have sought solutions in various places like here, here, here, etc... However, I am unable to get it to work in my specific case: var timer_slideshow = {}; var that, that_boss, has_auto, el ...

Changing the URL dynamically based on user interaction with jQueryLet's leverage the power of

In continuation to my previous post, I am looking to dynamically change a URL based on the selected language. For example, if the current URL is href="../folder/Languages/English/test/test.html, clicking or selecting another language should update it to: h ...

Tips for avoiding identical selections in multiple drop-down menus within a table and sending the data to the server via a POST request

For instance: visualizing a chart of infants, where each row the user chooses baby GenderDropDown, then picks a name. It's crucial to prevent duplicate name selections throughout the chart. If a user selects Male & John, that choice should not be ...

Utilizing Selenium for the Upload of an APK Document

I've encountered an issue during my test. I'm attempting to upload an APK file, but the process seems to halt at that stage. My attempts with simple sendKeys using the file path and an AutoIT script have both proven unsuccessful. Below is my in ...

Measuring the rendering time of an AngularJS directive

Is there a way to measure the rendering time of a directive (element)? If not, is it possible to identify which directive takes the longest time to render? PS. I have tried using Batarang, but it only displays watch-expressions that consume the most time. ...

How to use jQuery to delete the final table in an entire HTML document

This situation is really frustrating: When I make a jQuery Ajax call, the success callback returns an entire HTML page. The returned page looks like this: <html> <head> <title>Title</title> <body> <table></table> ...

The arrangement and presentation of div elements on a website

I am experiencing difficulties with the order of my divs and the buttons are not functioning properly. My goal is to display a div containing extensive content, and once a user clicks the begin test button located below the lengthy text, the content should ...

Is there a way to run Javascript using an ExtJS AJAX request without using eval() like how it can be done with JQuery?

I need to run JavaScript on pages loaded via Ext.Ajax.request. In order to achieve this, I must load the scripts and execute them using eval(), as shown below: Ext.Ajax.request({ url: 'content/view_application.php', success: function(obj ...

Alignment of text to the right in a two-column div design

I'm trying to figure out the most effective way to have a block of text aligned on the left and another block of text aligned on the right within a div. Here is an image that illustrates how I want it to appear Initially, I considered using three se ...

Configuring Angular routes based on service method invocation

I have my routes configured in @NgModule. I also have a service that determines which parts of the application to display based on specific conditions. I need to call this service and adjust the routes according to its output. Issue: The route configurati ...

Is there a way to monitor real-time updates without relying on setInterval or timeout functions?

Currently in the process of building a social network, I am working on fetching live notifications. The current approach involves sending an AJAX request every few seconds using setInterval. The code snippet for this operation is as follows: setInterval ( ...

Controlling the toggling of Bootstrap switch with jQuery

As I work with the bootstrap switch feature, I find myself facing a challenge due to the lack of robust documentation or accessible examples. <div class="col-xs-6"> <div class="pull-left"> <input class="switch" type="checkbox" id="cb ...

Mechanism for creating variables in Angular through factory services

While delving into the world of angular1, I encountered services and factories. Using the .factory() method, I passed in a string that represented the desired service name. Surprisingly, I found myself able to reference a variable matching this string wi ...

What could be causing my HTML website to load slowly and freeze when I switch to another program?

I am in the process of creating three websites. Two of them are functioning perfectly fine, but I am encountering a speed issue with the third one. Sometimes it loads slowly initially, but if I wait a bit longer, it eventually loads and works well. However ...

Hierarchical selectors do not function properly with styled components

Trying to get the hang of styled components, but struggling with targeting CSS classes in hierarchy. Specifically, I want to apply styles when a user hovers over navigation links. Below is my navbar code: import React from "react"; import { Nav, Navbar } ...