Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- body{
- background-color: #333;
- height: 100vh;
- display: flex;
- align-items: center;
- justify-content: center;
- gap: 1rem;
- flex-direction: column;
- transition: .5s;
- }
- .change-color{
- background-color: cadetblue;
- }
- button{
- font-size: 3rem;
- padding: 1rem;
- }
- .box{
- width: 300px;
- height: 300px;
- background-color: cornflowerblue;
- transition: 0.5s;
- }
- .change-color-caja{
- background-color: darkgoldenrod;
- }
- </style>
- </head>
- <body>
- <button id="boton">Click me</button>
- <div class="box" id="box"></div>
- <script>
- const boton = document.getElementById("boton");
- 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
- //Los demás elementos hay que seleccionarlos por ID, por clase, etc. En este caso por ID
- boton.addEventListener("click", () =>{
- document.body.classList.toggle("change-color");
- 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
- })
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement