|
12 | 12 | body {
|
13 | 13 | margin: 0;
|
14 | 14 | height: inherit;
|
| 15 | + display: flex; |
| 16 | + justify-content: center; |
| 17 | + align-items: center; |
15 | 18 | }
|
16 | 19 |
|
17 | 20 | div {
|
|
32 | 35 | <script>
|
33 | 36 | const divElem = document.querySelector('div');
|
34 | 37 |
|
35 |
| - document.body.addEventListener('mousemove', evt => { |
36 |
| - let anim = divElem.animate( |
37 |
| - { transform: `translate(${ evt.clientX}px, ${evt.clientY}px)` }, |
38 |
| - { duration: 500, fill: 'forwards' } |
39 |
| - ); |
40 |
| - |
41 |
| - // commitStyles() writes the end state of the animation to the animated |
42 |
| - //element inside a style attribute |
43 |
| - anim.commitStyles(); |
44 |
| - |
45 |
| - // If you explicity want the animations to be retained, then you can invoke persist() |
46 |
| - // But don't do this unless you really need to — having a huge list of animations |
47 |
| - // persisted can cause a memory leak |
48 |
| - //anim.persist() |
49 |
| - |
50 |
| - // onremove allows you to run an event handler that fires when the animation |
51 |
| - // was removed (i.e. put into an active replace state) |
52 |
| - anim.onremove = function() { |
53 |
| - console.log('Animation removed'); |
54 |
| - } |
55 |
| - |
56 |
| - // replaceState allows you to query an element to see what its replace state is |
57 |
| - // It will be active by default, or persisted if persist() was invoked |
58 |
| - console.log(anim.replaceState); |
59 |
| - }); |
| 38 | + // Keyframes |
| 39 | + let growShrink = [ |
| 40 | + { transform: 'scale(1)' }, |
| 41 | + { transform: 'scale(1.5)' } |
| 42 | + ]; |
| 43 | + |
| 44 | + let rotate360 = [ |
| 45 | + { transform: 'rotate(0deg)' }, |
| 46 | + { transform: 'rotate(360deg)' } |
| 47 | + ]; |
| 48 | + |
| 49 | + let moveLeftRight = [ |
| 50 | + { transform: 'translateX(0)' }, |
| 51 | + { transform: 'translateX(300px)' }, |
| 52 | + { transform: 'translateX(0)' } |
| 53 | + ]; |
| 54 | + |
| 55 | + // timing properties |
| 56 | + let slowInfinite = { |
| 57 | + duration: 5000, |
| 58 | + iterations: Infinity, |
| 59 | + fill: 'forwards' |
| 60 | + } |
| 61 | + |
| 62 | + let slowTwice = { |
| 63 | + duration: 3000, |
| 64 | + iterations: 2, |
| 65 | + fill: 'forwards' |
| 66 | + } |
| 67 | + |
| 68 | + let animation1 = divElem.animate( growShrink, slowTwice ); |
| 69 | + // commitStyles() |
| 70 | + animation1.commitStyles(); |
| 71 | + |
| 72 | + |
| 73 | + let animation2 = divElem.animate( rotate360, slowInfinite ); |
| 74 | + animation2.addEventListener('remove', () => { |
| 75 | + console.log('Animation 2 removed'); |
| 76 | + }) |
| 77 | + // persist() |
| 78 | + animation2.persist(); |
| 79 | + |
| 80 | + let animation3 = divElem.animate( moveLeftRight, slowInfinite ); |
| 81 | + |
| 82 | + console.log(animation1.replaceState); |
| 83 | + console.log(animation2.replaceState); |
| 84 | + console.log(animation3.replaceState); |
60 | 85 |
|
61 | 86 | </script>
|
62 | 87 | </body>
|
|
0 commit comments