Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
Trying To Make A Div Disapear And Reapear OnClick

I am trying to make a game and I have been trying to get the character button to disappear and reappear on click. I think the if else statements is the best way to do it but I am probably wrong because I am new to javascript. I managed to make it disappear but couldn't make it appear again on click html:


    <body>
    <div id="game">
        <div id="block"></div>
        <button id="character" onclick="myFunction()"></button>
    </div>
    <script>
        function myFunction() {
            if (document.getElementById("character").style.display="block" == true) {
            document.getElementById("character").style.display="none";
            } else {
                document.getElementById("character").style.display="block";
            }
        }
        
            

        





    </script>
    
    </body>
</html>

css:


    *{
    padding: 0;
    margin: 0;
}
#game {
    margin: auto;
    width: 400px;
    height: 500px;
    border: 1px solid black;
    overflow: hidden;
    
}
#block {
    width: 50px;
    height: 500px; 
    background-color: black;
    position: relative;
    left: 400px;
    animation: block 2s infinite linear;
}
@keyframes block {
    0%{left: 400px;}
    100%{left: -50px;}
}
#character {
    height: 50px;
    width: 50px;
    background-color: black;
    margin: auto;
    top: 250px;
   margin-left: 15px;
    position: absolute;
    display: block;
}

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

let x = 0;
document.getElementsByTagName('body')[0].addEventListener('click',function(){

   let char = document.getElementById('character')
   if(x%2 == 0){
      x++;
      char.classList.remove('show')
      char.classList.add('hide')
    }else{
       x++
       char.classList.remove('hide')
       char.classList.add('show')
    }

})
.hide{
  display:none;
 }
.show{
  display:block;
  }


    *{
    padding: 0;
    margin: 0;
}
#game {
    margin: auto;
    width: 400px;
    height: 500px;
    border: 1px solid black;
    overflow: hidden;
    
}
#block {
    width: 50px;
    height: 500px; 
    background-color: black;
    position: relative;
    left: 400px;
    animation: block 2s infinite linear;
}
@keyframes block {
    0%{left: 400px;}
    100%{left: -50px;}
}
#character {
    height: 50px;
    width: 50px;
    background-color: black;
    margin: auto;
    top: 250px;
   margin-left: 15px;
    position: absolute;
   
}
    <body>
    <div id="game">
        <div id="block"></div>
        <button id="character" class='show'></button>
    </div>
    
    
    </body>

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this:

    <body>
    <div id="game">
        <div id="block"></div>
        <button id="character" onclick="myFunction()"></button>
    </div>
    <script>
        function myFunction() {
            if (document.getElementById("character").style.display==="block") {
            document.getElementById("character").style.display="none";
            } else {
                document.getElementById("character").style.display="block";
            }
        }
    </script>
    
    </body>
</html>
about 4 years ago · Juan Pablo Isaza Relatório

0

What are you going to click in order to show the hidden box,since you have made it disappear ?

I created this snippet below to explain the logic you could follow in order to toggle between visible and hidden black boxes,you definitely need to click something to initiate visibility for the desired elements so i created a button for that.

function showElements(arr) accepts an array of id's you want to bring them back to page.

.black-box {
    height: 50px;
    width: 50px;
    background-color: black;
    position: relative;
    display: block;
    margin:5px;
    float: left;
}
<html>
<body>
    <div id="game">
        <div id="block"></div>
    <button onclick="showElements(['character','character2'])">SHOW ELEMENTS</button>
    <button class="black-box" id="character" onclick="hideThisElement(this)" style="display:block"></button>
  <button class="black-box" id="character2" onclick="hideThisElement(this)" style="display:block"></button>
    </div>
    <script defer>
        function hideThisElement(e){
    e.style.display = "none";
    }
function showElements(arr){

arr.forEach(el => {

let elId = document.getElementById(el)

if(document.body.contains(elId)){

if(elId.style.display == "none"){
elId.style.display = "block"
}


}
})

}

    </script>

</body>
</html>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda