|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <title> |
| 6 | + Four-Sided Positioning Plays Nicely With Scale() Transformations In CSS |
| 7 | + </title> |
| 8 | + <link rel="stylesheet" type="text/css" href="./demo.css"> |
| 9 | + <style type="text/css"> |
| 10 | + |
| 11 | + .modal { |
| 12 | + /* Four-sided positioning. */ |
| 13 | + bottom: 0px ; |
| 14 | + left: 0px ; |
| 15 | + position: fixed ; |
| 16 | + right: 0px ; |
| 17 | + top: 0px ; |
| 18 | + |
| 19 | + /* |
| 20 | + Animating the modal element into view: our modal-enter keyframes are |
| 21 | + going to use SCALE() (and opacity) to gradually transition the modal |
| 22 | + element into its fixed position layout. |
| 23 | + */ |
| 24 | + animation-duration: 1s ; /* NOTE: Absurdly SLOW for demo purposes. */ |
| 25 | + animation-fill-mode: both ; |
| 26 | + animation-iteration-count: 1 ; |
| 27 | + animation-name: modal-enter ; |
| 28 | + animation-timing-function: ease-out ; |
| 29 | + } |
| 30 | + |
| 31 | + @keyframes modal-enter { |
| 32 | + from { |
| 33 | + opacity: 0 ; |
| 34 | + transform: scale( 0.7 ) ; |
| 35 | + } |
| 36 | + to { |
| 37 | + opacity: 1 ; |
| 38 | + transform: scale( 1.0 ) ; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + </style> |
| 43 | +</head> |
| 44 | +<body> |
| 45 | + |
| 46 | + <h1> |
| 47 | + Four-Sided Positioning Plays Nicely With Scale() Transformations In CSS |
| 48 | + </h1> |
| 49 | + |
| 50 | + <p> |
| 51 | + <a class="toggle">Open modal</a> |
| 52 | + </p> |
| 53 | + |
| 54 | + <!-- |
| 55 | + This modal window will use FIXED positioning and have a four-sided (top, right, |
| 56 | + bottom, left) arrangement. It will also fade into view using CSS transitions. |
| 57 | + --> |
| 58 | + <template> |
| 59 | + <div class="modal"> |
| 60 | + <a class="toggle">Close modal</a> |
| 61 | + </div> |
| 62 | + </template> |
| 63 | + |
| 64 | + |
| 65 | + <!-- Load scripts. --> |
| 66 | + <script type="text/javascript" src="../../vendor/jquery/3.6.0/jquery-3.6.0.min.js"></script> |
| 67 | + <script type="text/javascript"> |
| 68 | + |
| 69 | + var modal = null; |
| 70 | + var template = $( "template" ); |
| 71 | + |
| 72 | + // We'll use event-delegation so that we can capture the click event in the |
| 73 | + // modal, which isn't even rendered yet. |
| 74 | + $( document ).on( "click", ".toggle", toggleModal ); |
| 75 | + |
| 76 | + // I show / hide the modal window by adding it to or removing it from the DOM |
| 77 | + // (Document Object Model) tree, respectively. |
| 78 | + function toggleModal() { |
| 79 | + |
| 80 | + if ( modal ) { |
| 81 | + |
| 82 | + modal.remove(); |
| 83 | + modal = null; |
| 84 | + |
| 85 | + } else { |
| 86 | + |
| 87 | + modal = $( template.prop( "content" ).firstElementChild.cloneNode( true ) ) |
| 88 | + .appendTo( document.body ) |
| 89 | + ; |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + </script> |
| 96 | + |
| 97 | +</body> |
| 98 | +</html> |
0 commit comments