Zenit

Untitled

Sep 14th, 2021 (edited)
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.55 KB | None | 0 0
  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>Document</title>
  8.     <style>
  9.         body{
  10.             background-color: #333;
  11.             height: 100vh;
  12.             display: flex;
  13.             align-items: center;
  14.             justify-content: center;
  15.             transition: .5s;
  16.         }
  17.         .change-color{
  18.             background-color: cadetblue;
  19.         }
  20.  
  21.         button{
  22.             font-size: 3rem;
  23.             padding: 1rem;
  24.         }
  25.     </style>
  26. </head>
  27. <body>
  28.     <button id="boton">Click me</button>
  29.  
  30.     <script>
  31.    
  32.         const boton = document.getElementById("boton");         //Creamos una variable llamada botón, aunque le puedes dar el nombre que tú quieras. Yo le llamo botón porque es un botón.
  33.                                                                 //En la variable botón guardamos el botón que tenemos en el html (document), y lo seleccionamos usando el ID (por eso el getElementById)
  34.  
  35.         boton.addEventListener("click", () =>{                  //Añadimos un listener al botón. No hace falta que lo sepas ahora mismo, pero un listener sirve para escuchar eventos. En este caso escuchamos el click
  36.             document.body.classList.toggle("change-color");     //Cuando el botón registra un click se activa esta función (código que hace algo), y en este caso le añadimos y quitamos la clase "change-color" al body.
  37.         })
  38.  
  39.         //Fíjate en la lógica de la instrucción. Primero accedemos al Document (<html>), luego al body, luego a la lista de clases del body y luego con la función toggle se la ponemos y quitamos de forma alterna.
  40.  
  41.     </script>
  42. </body>
  43. </html>
Add Comment
Please, Sign In to add comment