1#Examples of using Web Animations 2 3Property indexed keyframes syntax 4--------------------------------- 5- Each CSS property specifies its keyframe values as a list, different properties may have differently sized lists. 6- The `easing` property applies its timing function to all keyframes. 7 8[**Live demo**](http://jsbin.com/qiyeriruru/edit?js,output) 9```javascript 10element.animate({ 11 transform: [ 12 'scaleY(0.5)', 13 'scaleX(0.5)', 14 'scaleY(0.5)', 15 ], 16 background: [ 17 'red', 18 'blue', 19 'orange', 20 'red', 21 ], 22 easing: 'ease-in-out', 23}, { 24 duration: 2000, 25 iterations: Infinity, 26}); 27``` 28 29Keyframe list syntax 30-------------------- 31- Keyframes can be specified as a list of multiple CSS property values. 32- Individual keyframes can be given specific offsets and easings. 33- Not all properties need to be specified in every keyframe. 34- Offsets are implicitly distributed if not specified. 35 36[**Live demo**](http://jsbin.com/yajatoyere/edit?js,output) 37```javascript 38element.animate([ 39 { 40 background: 'red', 41 transform: 'none', 42 easing: 'ease-out', 43 }, 44 { 45 offset: 0.1, 46 transform: 'translateY(100px)', 47 easing: 'ease-in-out', 48 }, 49 { 50 offset: 0.2, 51 transform: 'translate(100px, 100px)', 52 easing: 'ease-in-out', 53 }, 54 { 55 offset: 0.4, 56 transform: 'translateX(100px)', 57 easing: 'ease-out', 58 }, 59 { 60 background: 'blue', 61 transform: 'none', 62 }, 63], { 64 duration: 4000, 65 iterations: Infinity, 66}); 67``` 68 69Timing parameters 70----------------- 71- Web Animations inherits many of its timing parameters from CSS Animations. 72- See the [specification](http://w3c.github.io/web-animations/#animationeffecttimingreadonly) for details on each parameter. 73 74[**Live demo**](http://jsbin.com/dabehipiyo/edit?js,output) 75```javascript 76element.animate({ 77 transform: ['none', 'translateX(100px)'], 78 background: ['green', 'lime'], 79}, { 80 // Apply effect during delay. 81 fill: 'backwards', 82 83 // Delay starting by 500ms. 84 delay: 500, 85 86 // Iterations last for 2000ms. 87 duration: 2000, 88 89 // Start at 25% through an iteration. 90 iterationStart: 0.25, 91 92 // Run for 2 iterations. 93 iterations: 2, 94 95 // Play every second iteration backwards. 96 direction: 'alternate', 97 98 // Stop animating 500ms earlier. 99 endDelay: -500, 100 101 // The timing function to use with each iteration. 102 easing: 'ease-in-out', 103}); 104``` 105 106Playback controls 107----------------- 108- element.animate() returns an Animation object with basic playback controls. 109- See the [specification](http://w3c.github.io/web-animations/#the-animation-interface) for details on each method. 110 111[**Live demo**](http://jsbin.com/kutaqoxejo/edit?js,output) 112```javascript 113var animation = element.animate({ 114 transform: ['none', 'translateX(200px)'], 115 background: ['red', 'orange'], 116}, { 117 duration: 4000, 118 fill: 'both', 119}); 120animation.play(); 121animation.reverse(); 122animation.pause(); 123animation.currentTime = 2000; 124animation.playbackRate += 0.25; 125animation.playbackRate -= 0.25; 126animation.finish(); 127animation.cancel(); 128``` 129 130Transitioning states with element.animate() 131------------------------------------------- 132- This is an example of how to animate from one state to another using Web Animations. 133 134[**Live demo**](http://jsbin.com/musufiwule/edit?js,output) 135```javascript 136var isOpen = false; 137var openHeight = '100px'; 138var closedHeight = '0px'; 139var duration = 300; 140 141button.addEventListener('click', function() { 142 // Prevent clicks while we transition states. 143 button.disabled = true; 144 button.textContent = '...'; 145 146 // Determine where we're animation from/to. 147 var fromHeight = isOpen ? openHeight : closedHeight; 148 var toHeight = isOpen ? closedHeight : openHeight; 149 150 // Start an animation transitioning from our current state to the final state. 151 var animation = element.animate({ height: [fromHeight, toHeight] }, duration); 152 153 // Update the button once the animation finishes. 154 animation.onfinish = function() { 155 isOpen = !isOpen; 156 button.textContent = isOpen ? 'Close' : 'Open'; 157 button.disabled = false; 158 }; 159 160 // Put our element in the final state. 161 // Inline styles are overridden by active animations. 162 // When the above animation finishes it will stop applying and 163 // the element's style will fall back onto this inline style value. 164 element.style.setProperty('height', toHeight); 165}); 166``` 167 168Generating animations 169--------------------- 170- The Javascript API allows for procedurally generating a diverse range of interesting animations. 171 172[**Live demo**](http://jsbin.com/xolacasiyu/edit?js,output) 173```html 174<!DOCTYPE html> 175<script src="https://rawgit.com/web-animations/web-animations-js/master/web-animations.min.js"></script> 176 177<style> 178#perspective { 179 margin-left: 100px; 180 width: 300px; 181 height: 300px; 182 perspective: 600px; 183} 184 185#container { 186 width: 300px; 187 height: 300px; 188 line-height: 0; 189 transform-style: preserve-3d; 190} 191 192.box { 193 display: inline-block; 194 width: 20px; 195 height: 20px; 196 background: black; 197} 198</style> 199 200<div id="perspective"> 201 <div id="container"></div> 202</div> 203 204<script> 205container.animate({ 206 transform: [ 207 'rotateX(70deg) rotateZ(0deg)', 208 'rotateX(70deg) rotateZ(360deg)', 209 ], 210}, { 211 duration: 20000, 212 iterations: Infinity, 213}); 214 215for (var y = -7; y <= 7; y++) { 216 for (var x = -7; x <= 7; x++) { 217 var box = createBox(); 218 box.animate({ 219 transform: [ 220 'translateZ(0px)', 221 'translateZ(20px)', 222 ], 223 opacity: [1, 0], 224 }, { 225 delay: (x*x + y*y) * 20, 226 duration: 2000, 227 iterations: Infinity, 228 direction: 'alternate', 229 easing: 'ease-in', 230 }); 231 } 232} 233 234function createBox() { 235 var box = document.createElement('div'); 236 box.className = 'box'; 237 container.appendChild(box); 238 return box; 239} 240</script> 241``` 242