Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do remove a class after revealing it?

I have a gif that I am trying to remove after 3 seconds of it being toggled on. Here is the breakdown.
• When the user clicks on the orange square it gets removed.
• A gif is toggled on.
• After 3 seconds
I want the gif to get removed and the brown circle toggle on.
I know I have to use the setTimeout method, but I do not know how to apply it. Can anyone help?
`

//Removes square
function remove(){
    this.remove();
    
}

//Adds gif
let gif = document.querySelector (".gif");

function showHide(){
    gif.classList.toggle("hide"); 
    
}

//Removes gif
*{
    padding:0;
    margin:0;
}
body{
    background-color:pink;
}
.parent{
    width:100vw;
    height:100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}


.gif{
    border-radius: 20px;
    z-index: 1; 
}
.hide{
 display:none;
}
#square{
    width:200px;
    height:200px;
    position: absolute;
    background-color:orange; 
    border-radius: 20px;
    z-index: 2; 
    top:50%;
    left:50%;
    transform: translate(-50%,-50%);
    cursor: pointer;
}

.circle{
    width: 200px;
    height:200px;
    background-color:brown;
    border-radius: 50%;   
}

.hide_2{
    display:none;
   }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> gif reveal </title>
    <link rel="stylesheet" href="styles.css">

</head>
<body>
    <div class="parent"> 
      <img class="gif hide" src="kuromi.gif">
        <div id="square" onclick="this.remove();showHide();">
            <div class="circle hide_2"></div>
        </div>    
       
    
    </div>
    <script src="index.js"></script>
    
</body>
</html>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The issue is occurring because you are trying to show the circle which is a child of square after deleting the square. So making the square a sister element of the circle would get your job done.

//Removes square
function remove() {
  this.remove();

}

//Adds gif
let gif = document.querySelector(".gif");
let circle = document.querySelector(".circle")

function showHide() {
  gif.classList.toggle("hide");
  setTimeout(() => {
    gif.classList.toggle("hide");
    circle.classList.toggle("hide");
  }, 3000)

}

//Removes gif
* {
  padding: 0;
  margin: 0;
}

body {
  background-color: pink;
}

.parent {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.gif {
  border-radius: 20px;
  z-index: 1;
}

.hide {
  display: none;
}

#container {
  z-index: 2;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  cursor: pointer;
  position: absolute;
}

#square {
  width: 200px;
  height: 200px;
  background-color: orange;
  border-radius: 20px;
}

.circle {
  width: 200px;
  height: 200px;
  background-color: brown;
  border-radius: 50%;
}

.hide_2 {
  display: none;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title> gif reveal </title>
  <link rel="stylesheet" href="styles.css">

</head>

<body>
  <div class="parent">
    <img class="gif hide" src="kuromi.gif">
    <div id="container">
      <div id="square" onclick="this.remove();showHide();"></div>
      <div class="circle hide"></div>
    </div>

  </div>
  <script src="index.js"></script>

</body>

</html>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading