17

I would like to be able to read the value of a CSS property in the middle of a transition before it is fully executed. Is that possible? So if during a transition from 0% to 100%, I were to check halfway through, could I see it at 50%?

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
TimE
  • 2,800
  • 1
  • 27
  • 25

2 Answers2

16

Is it possible to get the current css property during a transition in JavaScript?

Yes

var timer;

function test(e) {
    var $this;
    $this = $(this);
    timer = setInterval(function () {
        console.log($this.height());
    }, 500);
}
function untest(e) {
    clearInterval(timer);
}

$('div').mouseenter(test).mouseleave(untest);
div
{
    transition: height 10s;
    -moz-transition: height 10s;
    -webkit-transition: height 10s;
    width: 100px;
    height: 100px;
    background-color: #00F;
}

div:hover
{
    height: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>

So far I've only tested firefox & chrome, but it appears that you can get the current CSS height via JS.

I can't think of a reason why the browser wouldn't report the change in styles to the DOM during a CSS transition.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • 4
    It's [part of the spec](http://www.w3.org/TR/css3-transitions/#transitions-) too: "if a script queries the computed style of a property as it is transitioning, it will see an intermediate value that represents the current animated value of the property." – user123444555621 Jan 19 '12 at 04:41
  • 2
    I would like to note that this already good example can become Jquery free by using offsetHeight. – john-jones Dec 12 '14 at 11:24
  • @HermannIngjaldsson I've gone (I think) one step better in [my answer](https://stackoverflow.com/a/53072394/1709587) by not just eliminating jQuery but also demonstrating that this is possible for arbitrary CSS properties, and not just for `offsetHeight` (which isn't a CSS property, and as far as I know isn't precisely equivalent to any CSS property either). I did my own experiment and wrote my own answer because I found this one unpersuasive, since it seemed quite possible that `offsetHeight` would exhibit different behaviour to a real CSS property (though this turns out not to be the case). – Mark Amery Oct 30 '18 at 20:34
7

Yes, it's possible. The corresponding property on the object returned by getComputedStyle will change gradually over the course of a transition, as shown in this demo:

const box = document.getElementById('box'),
      turnBlueButton = document.getElementById('turnblue'),
      turnPinkButton = document.getElementById('turnpink'),
      computedStyleValueSpan = document.getElementById('computedstylevalue');
      
turnBlueButton.onclick = () => {
  box.classList.add('blue');
  box.classList.remove('pink');
}
turnPinkButton.onclick = () => {
  box.classList.add('pink');
  box.classList.remove('blue');
}

const computedStyleObj = getComputedStyle(box);

setInterval(() => {
  computedStyleValueSpan.textContent = computedStyleObj.backgroundColor;
}, 50);
#box {
  width:50px;
  height:50px;
  transition: background-color 10s;
}
.pink {
  background: pink;
}
.blue {
  background: blue;
}
<div id="box" class="pink"></div>

<p>
  <code>getComputedStyle(box).backgroundColor:</code>
  <code><span id="computedstylevalue"></span></code>
</p>

<button id="turnblue">Turn blue</button>
<button id="turnpink">Turn pink</button>

This behaviour is required by spec. https://www.w3.org/TR/css-transitions-1/#transitions- states:

The computed value of a property transitions over time from the old value to the new value. Therefore if a script queries the computed value of a property (or other data depending on it) as it is transitioning, it will see an intermediate value that represents the current animated value of the property.

While the Mozilla docs seem to say that getComputedStyle would return either the start or the end value, they seem wrong, at least for WebKit. WebKit-based browsers' getComputedStyle implementation seems to return intermediate values.

(Hat tip to https://stackoverflow.com/users/27862/user123444555621 for their comment pointing out the relevant spec passage.)

Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
  • @VicSeedoubleyew interesting nuance you've just added. Confusingly there seem to be two senses in which the idea of "final" property values are used in the notes at https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle, but yeah, the sentence *"During CSS transitions, `getComputedStyle` returns the original property value in Firefox, but the final property value in WebKit."* definitely seems to be saying what you interpret it as saying. I guess that note is just wrong. Perhaps one of us should dig into the history, try and see if it was ever true, and update MDN. – Mark Amery Aug 25 '20 at 10:44
  • Right. I was wondering how to report such a problem indeed – Vic Seedoubleyew Aug 27 '20 at 09:44