Skip to content

Failed to execute ‘animate’ on ‘element’: parameter 1 is not of type ‘object’

this.animate = 
 function(name,color){

this.style.transform = name;

this.style.backgroundColor= color;

};

document.body.animate("translateY('120px')",'red');

Answer

As a minimum you need an animation object and a duration for the animation Element.animate(). And then I think you got some of your code upside down.

const animate = function(element, animationObj, colorStr) {
  element.animate(animationObj, {
    duration: 2000
  });
  element.style.backgroundColor = colorStr;
};

let testElm = document.getElementById("test");

animate(testElm, {
  transform: 'translateY(120px)'
}, 'red');
<div id="test">test<div>