| 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 2 |
| 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 | main { |
| 24 | |
| 25 | |
| 26 | |
| 27 | view-transition-name: main; |
| 28 | } |
| 29 | |
| 30 | @keyframes desplazaALaDerecha { |
| 31 | from { |
| 32 | transform: translateX(100vw); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | @keyframes desplazaALaIzquierda { |
| 37 | to { |
| 38 | transform: translateX(-100vw); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | ::view-transition-old(main) { |
| 43 | animation-name: |
| 44 | desplazaALaIzquierda; |
| 45 | animation-duration: 2s; |
| 46 | } |
| 47 | |
| 48 | ::view-transition-new(main) { |
| 49 | animation-name: |
| 50 | desplazaALaDerecha; |
| 51 | animation-duration: 2s; |
| 52 | } |
| 53 | </style> |
| 54 | |
| 55 | </head> |
| 56 | |
| 57 | <body> |
| 58 | |
| 59 | <nav> |
| 60 | <a href="inicio">Inicio</a> |
| 61 | <a href="acercade">Acerca de</a> |
| 62 | <a href="contacto">Contacto</a> |
| 63 | </nav> |
| 64 | |
| 65 | <main id="inicio"> |
| 66 | <h1> |
| 67 | Single Page Application 2 |
| 68 | </h1> |
| 69 | <p> |
| 70 | Esta es la página principal. |
| 71 | </p> |
| 72 | </main> |
| 73 | |
| 74 | <main id="acercade" hidden> |
| 75 | <h1>Acerca de</h1> |
| 76 | <p> |
| 77 | Esta es la página Acerca de. |
| 78 | </p> |
| 79 | </main> |
| 80 | |
| 81 | <main id="contacto" hidden> |
| 82 | <h1>Contacto</h1> |
| 83 | <p> |
| 84 | Esta es la página Contacto. |
| 85 | </p> |
| 86 | </main> |
| 87 | |
| 88 | <footer> |
| 89 | <p> |
| 90 | © 2025 |
| 91 | Gilberto Pacheco Gallegos. |
| 92 | </p> |
| 93 | </footer> |
| 94 | |
| 95 | <script> |
| 96 | |
| 97 | const titulos = new Map([ |
| 98 | [ |
| 99 | "inicio", |
| 100 | "Single Page Application 2" |
| 101 | ], |
| 102 | [ |
| 103 | "acercade", |
| 104 | "Acerca de - Single Page Application 2" |
| 105 | ], |
| 106 | [ |
| 107 | "contacto", |
| 108 | "Contacto - Single Page Application 2" |
| 109 | ], |
| 110 | ]) |
| 111 | |
| 112 | const vistas = |
| 113 | [inicio, acercade, contacto] |
| 114 | |
| 115 | document.addEventListener("click", |
| 116 | event => { |
| 117 | event.preventDefault() |
| 118 | const target = event.target |
| 119 | if ( |
| 120 | target instanceof HTMLAnchorElement |
| 121 | ) { |
| 122 | try { |
| 123 | const href = target.href |
| 124 | const estado = |
| 125 | href.substring( |
| 126 | href.lastIndexOf("/") + 1 |
| 127 | ) |
| 128 | transicion(estado) |
| 129 | history.pushState( |
| 130 | estado, "", location.href |
| 131 | ) |
| 132 | } catch (err) { |
| 133 | console.error(err) |
| 134 | } |
| 135 | } |
| 136 | }) |
| 137 | |
| 138 | window.addEventListener( |
| 139 | "popstate", |
| 140 | event => { |
| 141 | if ( |
| 142 | typeof event.state === "string" |
| 143 | ) { |
| 144 | transicion(event.state) |
| 145 | } |
| 146 | } |
| 147 | ) |
| 148 | |
| 149 | history.replaceState( |
| 150 | "inicio", "", location.href |
| 151 | ) |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | function muestraVista(estado) { |
| 157 | document.title = |
| 158 | titulos.get(estado) ?? "" |
| 159 | for (const vista of vistas) { |
| 160 | vista.hidden = |
| 161 | estado !== vista.id |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | |
| 166 | |
| 167 | function transicion(estado) { |
| 168 | if ( |
| 169 | typeof document.startViewTransition === "function" |
| 170 | ) { |
| 171 | document.startViewTransition( |
| 172 | () => muestraVista(estado) |
| 173 | ) |
| 174 | } else { |
| 175 | muestraVista(estado) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | </script> |
| 180 | |
| 181 | </body> |
| 182 | |
| 183 | </html> |