Is there an elegant way to predict the dimension an element will have after the elements transition is complete?
Example:
HTML:
<div id="demo">...</div>
CSS:
#demo {
max-height: 0;
overflow:hidden;
transition: 2s max-height;
}
#demo.expand {
max-height: 1000px;
}
JS:
var demo = document.getElementById('demo');
demo.className = 'expand';
// Unfortunately the result will be 0px
// because the current height is measured
alert(demo.offsetHeight);
Demo:
codepen
We can't transition height , but we can transition max-height , since it has an explicit value. At any given moment, the actual height of the element will be the minimum of the height and the max-height .
The transition-duration property defines the length of time that a transition takes.
You must set an initial left value for form-container . If there is no initial left value the browser doesn't know from where to animate to the set hover value. This should fix your issue.
The height of an element is one CSS property that often needs to be transitioned. Sometimes, we want a part of an element to be collapsed until it is needed. That is, when a button is clicked, the height of an element increases or decreases. See more buttons and bootstrap panels make use of this technique.
You try it out, and… the height doesn’t transition. It snaps between the two sizes as if transition had never been set. After some fiddling, you figure out that this problem only happens when the height starts out or ends up as auto. Percentages, pixel values, any absolute units work as expected.
If you’ve ever tried to use a CSS transition on an element with the hidden attribute or display: none;, you know this can be a challenge. I’ve run into this problem a number of times and decided to write an npm package to provide a reusable solution. There are a ton of ways to hide HTML elements.
When the state of an element is changed, it's pretty cool to have a visual effect to show that an action occurred. Thanks to CSS transitions, we have a wide range of transition effects that can be used on our HTML elements. The height of an element is one CSS property that often needs to be transitioned.
Is there an elegant way to measure the dimension an element will have after the elements transition is complete?
You can use the transitionend
event to look at the height of your element after the transition is finished:
$('#demo').on("transitionend", function(e) {
console.log($('#demo').height());
}).addClass('expand');
This will get you a value of 20
, which I assume is what you are looking for?
jsFiddle
You can check the requested rendering height/width of content through the scrollHeight
and scrollWidth
properties.
e.g. try adding alert(demo.scrollHeight);
to the JS pane.
BlakeGru's answer works great for the OP's question, but doesn't work for similar problems that I've encountered. For completeness, and in case it's useful for others, here is a general strategy for finding the final dimensions an element will have once any transitions have been completed, before said transitions actually begin.
This example changes the width of an element (with a transition) and gets the height (which is determined automatically by content) that the element will have once the transition has finished.
function resize(element, endingWidth) {
// First, save any set transitions for later.
var transitions = window.getComputedStyle(element).transition;
// Now, disable any transitions so that our calculations will work properly.
element.style.transition = 'none';
// Get our current width.
var startingWidth = element.offsetWidth + 'px';
// Set a new width.
element.style.width = endingWidth;
// And get the element height. Because there are no transitions set, this will be the same height as at the end of any transitions.
var endingHeight = element.offsetHeight;
/*
* Now do whatever calculations we want with the ending height.
*/
alert(endingHeight);
// Set the element's width back to when we started, so we have a start point for our transition.
element.style.width = startingWidth;
// Force the browser to recalculate the element's dimensions. This seemingly pointless call is absolutely critical for the transition to work.
element.offsetWidth;
// Now, we can set our desired transitions and the ending width again, and we're away.
element.style.transition = transitions;
element.style.width = endingWidth;
}
Fiddle
The general idea is:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With