Advertisement
Zenit

Untitled

Sep 14th, 2021
1,254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.54 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.             gap: 1rem;
  16.             flex-direction: column;
  17.             transition: .5s;
  18.         }
  19.         .change-color{
  20.             background-color: cadetblue;
  21.         }
  22.  
  23.         button{
  24.             font-size: 3rem;
  25.             padding: 1rem;
  26.         }
  27.  
  28.         .box{
  29.             width: 300px;
  30.             height: 300px;
  31.             background-color: cornflowerblue;
  32.             transition: 0.5s;
  33.         }
  34.         .change-color-caja{
  35.             background-color: darkgoldenrod;
  36.         }
  37.     </style>
  38. </head>
  39. <body>
  40.     <button id="boton">Click me</button>
  41.  
  42.     <div class="box" id="box"></div>
  43.  
  44.     <script>
  45.    
  46.         const boton = document.getElementById("boton");                                                                    
  47.         const caja = document.getElementById("box");                //Aparte del botón, nos creamos la variable caja para poder acceder a ella, pues lo de document.body solo sirve para el body, no para otros elemenetos
  48.                                                                     //Los demás elementos hay que seleccionarlos por ID, por clase, etc. En este caso por ID              
  49.  
  50.         boton.addEventListener("click", () =>{                 
  51.             document.body.classList.toggle("change-color");      
  52.             caja.classList.toggle("change-color-caja");             //Lo mismo. En lugar de buscar el classList de document.body, vamos directamente al de caja porque la tenemos creada arriba. Y lo mismo, classList > toggle
  53.         })
  54.  
  55.        
  56.  
  57.     </script>
  58. </body>
  59. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement