1

Web Animation API supports keyframe animation, so you can go "from" and "to":

// Go from 0 to 300px
someElement.animate(
[
  { transform: 'translateY(0)' },
  { transform: 'translateY(300px)' }   
], {
  duration: 1000
});

Is there some way to go "to" without having to specify "from"? Something like:

// Go from the current position to 300px
someElement.animate({ transform: 'translateY(300px)' }, {
  duration: 1000
});

Or I must store the current position somewhere else?

Marvin3
  • 5,741
  • 8
  • 37
  • 45

1 Answers1

0

You can set transform property before to start the animation. Also, you can use <animatable/> WebComponent to modify and reset the value from start and finish events:

  • HTML:
<animatable-component delay="1000" duration="800" animation="slideInUp" easing="ease-in-out">
  <h1>Hello World</h1>
</animatable-component>
  • JS:
const animatableElement = document.querySelector('animatable-component')
animatableElement.addEventListener("start", function(event) {
  console.log('ANIMATION START', event);
  var startTransform = 'translateY(0px)'
  event.detail.style.transform = startTransform;
  event.detail.style.webkitTransform = startTransform;
});

animatableElement.addEventListener("finish", function(event) {
  console.log('ANIMATION FINISH', event);
  var endTransform = 'translateY(300px)'
  event.detail.style.transform = endTransform;
  event.detail.style.webkitTransform = endTransform;
});

More details here

jdnichollsc
  • 1,520
  • 1
  • 24
  • 44