|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <title> |
| 6 | + Applying Multiple Animation Keyframes To A Loading Indicator In CSS |
| 7 | + </title> |
| 8 | + |
| 9 | + <link rel="stylesheet" type="text/css" href="./demo.css" /> |
| 10 | +</head> |
| 11 | +<body> |
| 12 | + |
| 13 | + <h1> |
| 14 | + Applying Multiple Animation Keyframes To A Loading Indicator In CSS |
| 15 | + </h1> |
| 16 | + |
| 17 | + <p> |
| 18 | + <button class="action action--show"> |
| 19 | + Show loading spinner |
| 20 | + </button> |
| 21 | + <button class="action action--hide"> |
| 22 | + Hide loading spinner |
| 23 | + </button> |
| 24 | + </p> |
| 25 | + |
| 26 | + <div class="ingress"> |
| 27 | + <!-- Spinner will be injected here. --> |
| 28 | + </div> |
| 29 | + |
| 30 | + <template class="template"> |
| 31 | + <div class="spinner"> |
| 32 | + Loading.... |
| 33 | + </div> |
| 34 | + </template> |
| 35 | + |
| 36 | + <style type="text/css"> |
| 37 | + /* |
| 38 | + The first animation is here to provide a DELAYED RENDERING of the injected |
| 39 | + DOM element. This allows us to inject the spinner right away, but only show |
| 40 | + it the user if the content takes longer than "Xms" to load. |
| 41 | + */ |
| 42 | + @keyframes spinner-fade { |
| 43 | + from { |
| 44 | + opacity: 0.0 ; /* In DOM, but visually hidden. */ |
| 45 | + } |
| 46 | + to { |
| 47 | + opacity: 1.0 ; /* In DOM, and visible. */ |
| 48 | + } |
| 49 | + } |
| 50 | + /* The second animation is here to provide the ongoing pulsing of the spinner. */ |
| 51 | + @keyframes spinner-pulse { |
| 52 | + 0%, 100% { |
| 53 | + transform: translateX( 0px ) ; |
| 54 | + } |
| 55 | + 50% { |
| 56 | + transform: translateX( 10px ) ; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + .spinner { |
| 61 | + /* |
| 62 | + For our animation, we're going to attach TWO DIFFERENT animation |
| 63 | + keyframes using sets of comma-delimited settings. The first setting in |
| 64 | + each property will be applied to our FADE animation; the second setting |
| 65 | + in each property will be applied to our PULSE animation. We're using two |
| 66 | + animations because we want the first one (the fade in) to only run once |
| 67 | + and we want the second one (the pulse) to run infinitely. |
| 68 | + */ |
| 69 | + animation-name: |
| 70 | + spinner-fade, |
| 71 | + spinner-pulse |
| 72 | + ; |
| 73 | + animation-iteration-count: |
| 74 | + 1, /* The FADE animation should only run once and FILL forward. */ |
| 75 | + infinite /* The PULSE animation should repeat forever. */ |
| 76 | + ; |
| 77 | + /* |
| 78 | + For the purposes of the demo, we're going to use a LARGE DELAY so that |
| 79 | + the overall effect is easier to see. This 2000ms represents the time that |
| 80 | + the spinner is in the DOM, but still hidden from the user. |
| 81 | + */ |
| 82 | + animation-delay: 2000ms ; /* Same setting for both animations. */ |
| 83 | + animation-duration: 500ms ; /* Same setting for both animations. */ |
| 84 | + animation-fill-mode: both ; /* Same setting for both animations. */ |
| 85 | + } |
| 86 | + </style> |
| 87 | + |
| 88 | + <script type="text/javascript" src="../../vendor/jquery/3.6.0/jquery-3.6.0.min.js"></script> |
| 89 | + <script type="text/javascript"> |
| 90 | + |
| 91 | + var showButton = $( ".action--show" ); |
| 92 | + var hideButton = $( ".action--hide" ); |
| 93 | + var ingress = $( ".ingress" ); |
| 94 | + var template = $( ".template" ); |
| 95 | + |
| 96 | + showButton.click( |
| 97 | + function injectSpinner() { |
| 98 | + |
| 99 | + ingress.append( template.prop( "content" ).firstElementChild.cloneNode( true ) ); |
| 100 | + |
| 101 | + } |
| 102 | + ); |
| 103 | + hideButton.click( |
| 104 | + function removeSpinner() { |
| 105 | + |
| 106 | + ingress.empty(); |
| 107 | + |
| 108 | + } |
| 109 | + ); |
| 110 | + |
| 111 | + </script> |
| 112 | + |
| 113 | +</body> |
| 114 | +</html> |
0 commit comments