0

I am trying to append a new child when user clicks on a button. The new child is already defined with few CSS properties. Is it possible to do so ? I have tried a few codes, the best i could do is -

var body = document.querySelector('body');
var bubbles = document.createElement("span")

function a1click(){
      var size = Math.random() * 100;
      bubbles.style.width = 100 + size+'px';
      bubbles.style.height = 100 + size+'px';
      body.appendChild(bubbles);     
  }
*{
    margin: 0;
    padding: 0;
}
#a1{
    position: relative;
    top: 250px;
    left: 100px;
    width: 30px;
    height: 150px;
    border: 2px solid #fff;
    background: rgb(0, 0, 0);
    float: left;
    cursor: pointer;
    perspective: 600;
}
span{
    position: absolute;
    top: 120px;
    left: 60%;
    width: 50px;
    height: 50px;
    background: black;
    animation: tweek 1s linear;
    transform-origin: top;
    pointer-events: none;
}
@keyframes tweek {
    0% {
      transform: rotate(90deg) translate(300px);
    }
  
    100% {
      transform: rotate(0deg) translate(250px);
      opacity: 0;
    }
  }
<!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>Document</title>
</head>
<body onkeydown="keypress(event)">
    <div id="a1" onclick="a1click()"></div>

    <script src="script.js"></script>
</body>
</html>

This was good uptil here but the problem is that when we click button the box is getting appended continously, i want it to append only once if the button is clicked once if twice then again and so on.. Please help me..Any help will be appreciated.

Utkarsh Tyagi
  • 1,379
  • 1
  • 7
  • 12

2 Answers2

0

The problem is here:

 animation: tweek 1s linear infinite;

The "infinite" is an attribute called animation-iteration-count. It defaults to 1, but since you have it set to infinite, it will continue to show this animation looping forever.

iamjane
  • 162
  • 8
  • 1
    I have edited my post this time without infinite but a new problem arrises now. Please view the snippet for detail, also thanks for efforts :D – Utkarsh Tyagi Jun 24 '20 at 18:07
  • I'm not sure what you want to happen with your code. [This question](https://stackoverflow.com/questions/39198083/remove-hide-div-from-dom-after-animation-completes-using-css) explains a way to have the animation disappear when it is done, but if you want to destroy the div you'll have to add that in your JS. – iamjane Jun 24 '20 at 18:11
  • 1
    Thanks a lot dude u gave me a hint that helped me out.. – Utkarsh Tyagi Jun 24 '20 at 18:17
0

I have found the correct answer thanks to @iamjane who gave me the idea.. I have added timeout funtion which removes the element after a specific time..

var body = document.querySelector('body');
var bubbles = document.createElement("span")

function a1click(){
      var size = Math.random() * 100;
      bubbles.style.width = 100 + size+'px';
      bubbles.style.height = 100 + size+'px';
      body.appendChild(bubbles);
      setTimeout(function(){
        bubbles.remove();
      },1000)
  }
*{
    margin: 0;
    padding: 0;
}
#a1{
    position: relative;
    top: 250px;
    left: 100px;
    width: 30px;
    height: 150px;
    border: 2px solid #fff;
    background: rgb(0, 0, 0);
    float: left;
    cursor: pointer;
    perspective: 600;
}
span{
    position: absolute;
    top: 120px;
    left: 60%;
    width: 50px;
    height: 50px;
    background: black;
    animation: tweek 1s linear;
    transform-origin: top;
    pointer-events: none;
}
@keyframes tweek {
    0% {
      transform: rotate(90deg) translate(300px);
    }
  
    100% {
      transform: rotate(0deg) translate(250px);
      opacity: 0;
    }
  }
<!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>Document</title>
</head>
<body onkeydown="keypress(event)">
    <div id="a1" onclick="a1click()"></div>

    <script src="script.js"></script>
</body>
</html>
Utkarsh Tyagi
  • 1,379
  • 1
  • 7
  • 12