Skip to content Skip to sidebar Skip to footer

Get Css Value Mid-transition With Native Javascript

This question was asked before, but the answer uses jQuery, here. So, I am going to tweak the question to specifically ask for a native solution, to minimize dependencies. Let's

Solution 1:

It is very easy to port the jQuery script from the linked thread into its vanilla JavaScript equivalent and below is a sample. The output is printed on the right side (output#op element) once timer expires.

All that we are doing is the following:

  • Attach two event handlers to the element which triggers the transition (sometimes the triggering element can be different from the one that has animation). In the other thread, the element that is triggering the transition and the one that is being transitioned is the same. Here, I have put it on two different elements just for a different demo.
  • One event handler is for mouseover event and this creates a timer (using setTimeout) which gets the opacity and top value of the element that is being transitioned upon expiry of timer.
  • The other event handler is for mouseleave event to clear the timer when the user has hovered out before the specific point at which we need the opacity and top value to be obtained.
  • Getting the opacity and top value of the element that is being transitioned can be obtained by using the window.getComputedStyle method.
  • Unlike the demo in the other thread (which uses setInterval), here I have used setTimeout. The difference is that setInterval adds an interval and so the function is executed every x seconds whereas the function passed to setTimeout is executed only once after x seconds. You can use whichever fits your needs.

var wrap = document.querySelector('.wrapper'),
  el = document.querySelector('.with-transition'),
  op = document.querySelector('#op');
var tmr;

wrap.addEventListener('mouseenter', function() {
  tmr = setTimeout(function() {
    op.innerHTML = 'Opacity: ' + window.getComputedStyle(el).opacity +
      ', Top: ' + window.getComputedStyle(el).top;
  }, 2500);
});
wrap.addEventListener('mouseleave', function() {
  clearTimeout(tmr);
});
.wrapper {
  position: relative;
  height: 400px;
  width: 400px;
  background: yellowgreen;
}
.with-transition {
  position: absolute;
  top: 0px;
  left: 100px;
  width: 200px;
  height: 100px;
  background: yellow;
  opacity: 0;
  transition: all 5s linear;
}
.wrapper:hover.with-transition {
  top: 300px;
  opacity: 1;
}
output {
  position: absolute;
  top: 50px;
  right: 50px;
}
<divclass='wrapper'><divclass='with-transition'></div></div><outputid='op'></output>

Solution 2:

The answer referenced in the duplicate question is easily modified to NOT use jquery. There is no black magic happening there.

The real question is why would you want to do this?

If You need control over a transition just impliment the partial transition with javascript, do what you need, then complete the transition.

Post a Comment for "Get Css Value Mid-transition With Native Javascript"