|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 7 | + <title>Web Animations</title> |
| 8 | + <style> |
| 9 | + * { |
| 10 | + box-sizing: border-box; |
| 11 | + margin: 0; |
| 12 | + padding: 0; |
| 13 | + font-family: sans-serif; |
| 14 | + } |
| 15 | + |
| 16 | + main { |
| 17 | + background: hsl(0deg, 0%, 10%); |
| 18 | + width: 100vw; |
| 19 | + height: 100vh; |
| 20 | + display: grid; |
| 21 | + place-items: center; |
| 22 | + } |
| 23 | + |
| 24 | + .container { |
| 25 | + text-align: center; |
| 26 | + } |
| 27 | + |
| 28 | + h1 { |
| 29 | + font-size: 72px; |
| 30 | + color: white; |
| 31 | + } |
| 32 | + |
| 33 | + h2 { |
| 34 | + color: hsl(0deg, 0%, 80%); |
| 35 | + font-size: 18px; |
| 36 | + margin-top: 18px; |
| 37 | + } |
| 38 | + </style> |
| 39 | + </head> |
| 40 | + <body> |
| 41 | + <main> |
| 42 | + <div class="container"> |
| 43 | + <h1 class="title">Welcome</h1> |
| 44 | + <h2 class="subtitle">JavaScript Web Animations API</h2> |
| 45 | + </div> |
| 46 | + </main> |
| 47 | + <script> |
| 48 | + let title = document.querySelector(".title"); |
| 49 | + let subTitle = document.querySelector(".subtitle"); |
| 50 | + let fadeAndMove = [ |
| 51 | + { |
| 52 | + opacity: 0, |
| 53 | + transform: `translateY(-20px)`, |
| 54 | + }, |
| 55 | + { |
| 56 | + opacity: 1, |
| 57 | + transform: `translateY(0px)`, |
| 58 | + }, |
| 59 | + ]; |
| 60 | + |
| 61 | + let titleTiming = { |
| 62 | + duration: 2000, |
| 63 | + easing: "ease-in-out", |
| 64 | + }; |
| 65 | + |
| 66 | + const titleChange = title.animate(fadeAndMove, titleTiming); |
| 67 | + |
| 68 | + let expand = [ |
| 69 | + { |
| 70 | + letterSpacing: "-0.5em", |
| 71 | + opacity: 0, |
| 72 | + }, |
| 73 | + { |
| 74 | + letterSpacing: "initial", |
| 75 | + opacity: 1, |
| 76 | + }, |
| 77 | + ]; |
| 78 | + |
| 79 | + let subTitleTiming = { |
| 80 | + duration: titleChange.effect.getComputedTiming().duration / 2, |
| 81 | + easing: "ease-in-out", |
| 82 | + }; |
| 83 | + |
| 84 | + const subTitleChange = subTitle.animate(expand, subTitleTiming); |
| 85 | + subTitleChange.pause(); |
| 86 | + |
| 87 | + document.addEventListener("click", () => { |
| 88 | + // idle, running, paused, finished |
| 89 | + if (subTitleChange.playState !== "finished") { |
| 90 | + subTitleChange.play(); |
| 91 | + } |
| 92 | + }); |
| 93 | + </script> |
| 94 | + </body> |
| 95 | +</html> |
0 commit comments