English 中文(简体)
如何在披露后取消一个等级?
原标题:How do remove a class after revealing it?
  • 时间:2024-03-18 02:12:43
  •  标签:
  • javascript

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>
最佳回答

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>
问题回答

我想做的是:

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

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

然而,我倾向于不使用<代码>toggle<>/code>方法,因为有时这种方法可能只使用少量的电离。

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.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签