skip to Main Content

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>

2

Answers


  1. I would do something like:

    function showHide(){
        gif.classList.toggle("hide");    
    }
    
    function handleOnClick = () => {
        remove();
        showHide();
        setTimeout(() => showHide(), 3000); // This will get executed after 3 seconds (in milliseconds)
    }
    

    I, however, would prefer not to use toggle method as sometimes it might work a little bit quirky.

    const showHide = (hide) => {
      if (hide) gif.classList.add("hide");
      else gif.classList.remove("hide");
    };
    
    function handleOnClick = () => {
        remove();
        showHide(true);
        setTimeout(() => showHide(false), 3000);
    }
    

    It should work the same way, but this way it is clearer on what action you want to do.

    Login or Signup to reply.
  2. 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.

    //Adds gif
    let gif = document.querySelector(".gif");
    let circle = document.querySelector(".circle")
    let square = document.querySelector("#square");
    
    function showHide(elem) {
      if (elem == 'square') {
        square.classList.toggle("hide");
      } else {
        circle.classList.toggle("hide");
      }
      gif.classList.toggle("hide");
      setTimeout(() => {
        gif.classList.toggle("hide");
        if (elem == 'square') {
          circle.classList.toggle("hide");
        } else {
          square.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="showHide('square');"></div>
          <div class="circle hide" onclick="showHide('circle');"></div>
        </div>
    
      </div>
      <script src="index.js"></script>
    
    </body>
    
    </html>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search