0

I'm trying to get the Javascript animation to match exactly my css animation. I'm not sure why this is not working? I'm trying to use basic javascript. not jquery. The second div should be matching the animation of the first div

https://jsfiddle.net/JokerMartini/pf2nt4su/

let content = document.getElementById("content");

html = `<div class="base"></div>`;
let doc = new DOMParser().parseFromString(html, 'text/html');
let div = doc.body.firstChild;

// https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Using_the_Web_Animations_API
// keyframes
var keyframes = [{
  backgroundColor: 'green',
  opacity: 0
}, {
  backgroundColor: 'blue',
  opacity: 1
}];
// timing 
let timing = {
  duration: 2000,
  iterations: Infinity
}

div.animate([
  keyframes,
  timing
]);

//elements += html;
content.append(div)
body {
  background: #808080;
}

.base {
  width: 180px;
  height: 90px;
  background: black;
}

.sample {
  animation: play 2s steps(10) infinite;
}

@keyframes play {
  0% {
    background-color: green;
    opacity: 0;
  }

  100% {
    background-color: blue;
    opacity: 1;
  }
}
<div class='base sample'></div>
<div id="content"></div>

2
  • You have an error here: opacity: 1;, you should remove the ;. I am not sure if it's related to your issue Commented May 6, 2020 at 20:15
  • no that is not the issue Commented May 6, 2020 at 20:24

3 Answers 3

1

edit: Replace below (notice square brackets)

div.animate([
  keyframes,
  timing
]);

with:

document.getElementById("content").animate(
  keyframes,
  timing
);

Here working code snippet: (updated with steps in JS)

<style>
body {
  background: #808080;
}

.base {
  width: 180px;
  height: 90px;
  background: black;
}

.sample {
  animation: play 2s steps(8) infinite;
}

@keyframes play {
  0% {
    background-color: green;
    opacity: 0;
  }

  100% {
    background-color: blue;
    opacity: 1;
  }
}
</style>

<div class='base sample'></div> <br>
<div id="content" class="base"></div>
<script>
let content = document.getElementById("content");

html = `<div class="base"></div>`;
let doc = new DOMParser().parseFromString(html, 'text/html');
let div = doc.body.firstChild;

// https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Using_the_Web_Animations_API
// keyframes
var keyframes = [{
 backgroundColor: 'green',
  opacity: 0 
}, {
 backgroundColor: 'blue',
  opacity: 1
}];
// timing 
var timing = {
  duration: 2000,
   easing: 'steps(8)',
  iterations: Infinity
}



document.getElementById("content").animate(
  keyframes,
  timing
);



</script>

Sign up to request clarification or add additional context in comments.

3 Comments

in the css im setting a 'steps' value. how do i do that in the javascript
yes you can do that as well. I will update the code now
@JokerMartini please check now
0

Move the content.append(div) before the animate call. Also, remove the [] from the animate.

I forked your JSFiddle. Here is a working version. https://jsfiddle.net/jrgiant/c5z7x2bu/.

let content = document.getElementById("content");

html = `<div class="base"></div>`;
let doc = new DOMParser().parseFromString(html, 'text/html');
let div = doc.body.firstChild;

// https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Using_the_Web_Animations_API
// keyframes
var keyframes = [{
  backgroundColor: 'green',
  opacity: 0
}, {
  backgroundColor: 'blue',
  opacity: 1
}];
// timing 
let timing = {
  duration: 2000,
  iterations: Infinity
}
content.append(div)
div.animate(
  keyframes,
  timing
);

//elements += html;
body {
  background: #808080;
}

.base {
  width: 180px;
  height: 90px;
  background: black;
}

.sample {
  animation: play 2s steps(10) infinite;
}

@keyframes play {
  0% {
    background-color: green;
    opacity: 0;
  }

  100% {
    background-color: blue;
    opacity: 1;
  }
}
<div class='base sample'></div>
<div id="content"></div>

1 Comment

in the css im setting a 'steps' value. how do i do that in the javascript
0

Your exact problem is that you are applying the animation before appending the div, and passing the keyframes argument as a list of lists instead of a list of objects (as you defined it):

let content = document.getElementById("content");

html = `<div class="base"></div>`;
let doc = new DOMParser().parseFromString(html, 'text/html');
let div = doc.body.firstChild;
content.append(div)


// https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Using_the_Web_Animations_API
// keyframes
var keyframes = [{
  backgroundColor: 'green',
  opacity: 0
}, {
  backgroundColor: 'blue',
  opacity: 1
}];
// timing 
let timing = {
  duration: 200,
  iterations: Infinity
}

div.animate(
  keyframes,
  timing
);

1 Comment

in the css im setting a 'steps' value. how do i do that in the javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.