| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" |
| 8 | content="width=device-width"> |
| 9 | <title> |
| 10 | Single Page Application 1 |
| 11 | </title> |
| 12 | |
| 13 | <style> |
| 14 | nav { |
| 15 | display: flex; |
| 16 | align-items: center; |
| 17 | flex-wrap: wrap; |
| 18 | align-content: center; |
| 19 | list-style-type: none; |
| 20 | gap: 0.5rem; |
| 21 | } |
| 22 | |
| 23 | @keyframes aparece { |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | from { |
| 29 | opacity: 0; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | @keyframes desvanece { |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | to { |
| 40 | opacity: 0; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | ::view-transition-old(root) { |
| 51 | animation-name: desvanece; |
| 52 | animation-duration: 1s; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | ::view-transition-new(root) { |
| 63 | animation-name: aparece; |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | animation-delay: 1s; |
| 69 | animation-duration: 2s; |
| 70 | } |
| 71 | </style> |
| 72 | |
| 73 | </head> |
| 74 | |
| 75 | <body> |
| 76 | |
| 77 | <nav> |
| 78 | <a href="inicio">Inicio</a> |
| 79 | <a href="acercade">Acerca de</a> |
| 80 | <a href="contacto">Contacto</a> |
| 81 | </nav> |
| 82 | |
| 83 | <main id="inicio"> |
| 84 | <h1> |
| 85 | Single Page Application 1 |
| 86 | </h1> |
| 87 | <p> |
| 88 | Esta es la página principal. |
| 89 | </p> |
| 90 | </main> |
| 91 | |
| 92 | <main id="acercade" hidden> |
| 93 | <h1>Acerca de</h1> |
| 94 | <p> |
| 95 | Esta es la página Acerca de. |
| 96 | </p> |
| 97 | </main> |
| 98 | |
| 99 | <main id="contacto" hidden> |
| 100 | <h1>Contacto</h1> |
| 101 | <p> |
| 102 | Esta es la página Contacto. |
| 103 | </p> |
| 104 | </main> |
| 105 | |
| 106 | <footer> |
| 107 | <p> |
| 108 | © 2025 |
| 109 | Gilberto Pacheco Gallegos. |
| 110 | </p> |
| 111 | </footer> |
| 112 | |
| 113 | <script> |
| 114 | |
| 115 | |
| 116 | const titulos = new Map([ |
| 117 | [ |
| 118 | "inicio", |
| 119 | "Single Page Application 1" |
| 120 | ], |
| 121 | [ |
| 122 | "acercade", |
| 123 | "Acerca de - Single Page Application 1" |
| 124 | ], |
| 125 | [ |
| 126 | "contacto", |
| 127 | "Contacto - Single Page Application 1" |
| 128 | ], |
| 129 | ]) |
| 130 | |
| 131 | const vistas = |
| 132 | [inicio, acercade, contacto] |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | document.addEventListener( |
| 141 | "click", |
| 142 | event => { |
| 143 | event.preventDefault() |
| 144 | const target = event.target |
| 145 | if ( |
| 146 | target instanceof HTMLAnchorElement |
| 147 | ) { |
| 148 | try { |
| 149 | const href = target.href |
| 150 | |
| 151 | |
| 152 | const estado = |
| 153 | href.substring( |
| 154 | href.lastIndexOf("/") + 1 |
| 155 | ) |
| 156 | transicion(estado) |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | history.pushState( |
| 165 | estado, "", location.href |
| 166 | ) |
| 167 | } catch (err) { |
| 168 | console.error(err) |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | ) |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | window.addEventListener( |
| 179 | "popstate", |
| 180 | event => { |
| 181 | if ( |
| 182 | typeof event.state === "string" |
| 183 | ) { |
| 184 | transicion(event.state) |
| 185 | } |
| 186 | } |
| 187 | ) |
| 188 | |
| 189 | history.replaceState( |
| 190 | "inicio", "", location.href |
| 191 | ) |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | function muestraVista(estado) { |
| 197 | document.title = |
| 198 | titulos.get(estado) ?? "" |
| 199 | for (const vista of vistas) { |
| 200 | vista.hidden = |
| 201 | estado !== vista.id |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | function transicion(estado) { |
| 214 | if ( |
| 215 | typeof document.startViewTransition === "function" |
| 216 | ) { |
| 217 | document.startViewTransition( |
| 218 | () => muestraVista(estado) |
| 219 | ) |
| 220 | } else { |
| 221 | muestraVista(estado) |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | </script> |
| 226 | |
| 227 | </body> |
| 228 | |
| 229 | </html> |