0
I am studying Javascript and decided to create a practical project, which consists of a building that I can turn on and off the lights by clicking on the windows, until then ok.
But I wanted to implement the function of clicking on a button and changing from day to night and all the lights turn on automatically.
The part of changing from day to night got by clicking on the screen itself, but when I click to turn on or turn off some light keeps changing from day to night too.
I tried some different functions and a half, but I got caught up in it. Could someone please give a help?
HTML
document.querySelectorAll('.ligaDesliga').forEach(divs => {
divs.addEventListener('click', event => {
divs.classList.toggle('yellow');
})
})
let ambiente = document.querySelector('.day');
function alterarHorario(){
ambiente.addEventListener('click', alterar =>{
ambiente.classList.toggle('night')
})
}
alterarHorario();
*{
margin: 0;
padding: 0;
box-sizing: border-box;
overflow: hidden;
}
.day{
background-color: blue;
}
.night{
background-color: black;
}
.container{
display: flex;
justify-content: center;
align-items: center;
min-height: 110vh;
}
.container .predio{
display: flex;
flex-wrap: wrap;
align-content: space-between;
justify-content: center;
align-items: center;
height: 560px;
width: 250px;
border: 2px solid white;
background-color: gray;
padding: 20px;
}
#window1, #window2, #window3, #window4, #window5, #window6, #window7, #window8, #window9, #window10, #window11, #window12{
width: 85px;
height: 60px;
background: black;
cursor: pointer;
}
#window2, #window4, #window6, #window8, #window10, #window12{
margin-left: 10px;
}
.door{
width: 40px;
height: 80px;
background-color: brown;
position: relative;
bottom: -18px;
}
.yellow{
background-color: yellow !important;
box-shadow: 0 0 10px yellow,
0 0 15px yellow,
0 0 20px yellow;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Desafio prédio</title>
</head>
<body>
<div class="day">
<div class="container">
<div class="predio">
<div id="window1" class="ligaDesliga"></div>
<div id="window2" class="ligaDesliga"></div>
<div id="window3" class="ligaDesliga"></div>
<div id="window4" class="ligaDesliga"></div>
<div id="window5" class="ligaDesliga"></div>
<div id="window6" class="ligaDesliga"></div>
<div id="window7" class="ligaDesliga"></div>
<div id="window8" class="ligaDesliga"></div>
<div id="window9" class="ligaDesliga"></div>
<div id="window10" class="ligaDesliga"></div>
<div id="window11" class="ligaDesliga"></div>
<div id="window12" class="ligaDesliga"></div>
<div class="door"></div>
</div>
</div>
</div>
<script src="main.js"></script>
</body>
</html>
you are associating an event within a Function, this should be done outside of it, as the other event with the class "ligaDesliga"
– Ricardo Pontual
@Ricardopunctual applied in the same way as the other event, but when it is in night mode and I click on some light to turn on it is changing the background color also day and night.
– Marcos Prado