0

HTML

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>sample</title>
  </head>
  <style >
    body{
      background: black;
    }
    div{
      background : red;
      height: 200px;
      margin : 20px 100px;
      width : auto;
      display: none;
    }
    button{
      color: red;
      font-weight:bold;
      background : grey;
      padding : 5px 20px;
      font-size : 1rem;
    }
  </style>
  <body>
    <div id="myDIV">
    </div>
    <button type="button" onclick=myFunction() name="button">CLICK ME</button>
  </body>
  <script>
    function myFunction(){
      document.getElementById("myDIV").style.display = "block";
    }
  </script>
</html>

QUESTION
As you can see, when i click the button, the div appear. But this i want the dev to appear in a smooth manner.
I tried this solution but it did'nt work for me.

jeno
  • 13
  • 6

5 Answers5

1

You can use CSS transition to make it smooth.

If you want transitions you can't use display: none; to hide it because with display it is completely hidden or completely visible,
instead, you can use height or opacity.

To toggle its visibility, I also recommend toggling a class with the styles for the shown state instead of settings them directly in the script,
it makes it easier to maintain and to add more visual effects

function myFunction() {
  document.getElementById("myDIV").classList.toggle("shown")
}
body {
  background: black;
}

.shown {
  height: 200px;
  opacity: 1;
}

div {
  background: red;
  height: 0px;
  opacity: 0;
  margin: 20px 100px;
  width: auto;
  transition: all 1s;
}

button {
  color: red;
  font-weight: bold;
  background: grey;
  padding: 5px 20px;
  font-size: 1rem;
}
<div id="myDIV">
</div>
<button type="button" onclick=myFunction() name="button">CLICK ME</button>
rafalou38
  • 576
  • 1
  • 5
  • 16
0

Here is a solution using opacity. It seems like a good use case because it also eliminates content shifting in this example (the button changing position).

function myFunction(){
      document.getElementById("myDIV").classList.add('div-show');
    }
body{
      background: black;
    }
    div{
      background : red;
      height: 200px;
      margin : 20px 100px;
      width : auto;
      //display: none;
      opacity:0;
      transition:opacity.3s;
    }
    .div-show{
      opacity:1;
    }
    button{
      color: red;
      font-weight:bold;
      background : grey;
      padding : 5px 20px;
      font-size : 1rem;
    }
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>sample</title>
  </head>
  <body>
    <div id="myDIV">
    </div>
    <button type="button" onclick=myFunction() name="button">CLICK ME</button>
  </body>
</html>
ovelnel
  • 38
  • 4
  • Thanks for trying. But if we add the opacity. It only be invisible and still occupy space on the web page. I wanted a div to be created in a smooth manner when a button is clicked – jeno Mar 13 '21 at 06:21
0

Do you want it to fade in slowly? If so instead of using display use opacity and make it slowly increase with a css animation. Tutorial here https://www.w3schools.com/css/css3_animations.asp

GtiClicker
  • 78
  • 8
0

You can add css animation and keyframes. add the following inside your div in styles

animation-name: example; animation-duration: 2s;

and add the following inside style tag:

 @keyframes example {0%   {opacity: 0; } 100% {opacity: 1;}}

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>sample</title>
  </head>
  <style >
    body{
      background: black;
    }
    div{
      background : red;
      height: 200px;
      margin : 20px 100px;
      width : auto;
      display: none;
      animation-name: example;
  animation-duration: 2s;
    }

@keyframes example {
  0%   {opacity: 0; }
  100% {opacity: 1;}
}
    button{
      color: red;
      font-weight:bold;
      background : grey;
      padding : 5px 20px;
      font-size : 1rem;
    }
  </style>
  <body>
    <div id="myDIV">
    </div>
    <button type="button" onclick=myFunction() name="button">CLICK ME</button>
  </body>
  <script>
    function myFunction(){
      document.getElementById("myDIV").style.display = "block";
    }
  </script>
</html>
Oybek Toshmatov
  • 407
  • 5
  • 11
0

In the code you've pasted; the <style> tag is misplaced. For the <style> tag to work; you need to place it inside the <head> or <body> tag.

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8" />
  <title>sample</title>
  <style>
    body {
      background: black;
    }
    
    div#myDIV {
      background: red !important;
      font-szie: 50px;
      background: red;
      height: 200px;
      margin: 20px 100px;
      width: auto;
      opacity: 0;
      transition: 1s opacity;
    }
    
    div.animate__fadeIn {
      opacity: 1 !important;
      transition: 1s opacity;
    }
    
    button {
      color: red;
      font-weight: bold;
      background: grey;
      padding: 5px 20px;
      font-size: 1rem;
    }
  </style>
</head>

<body>
  
  <button type="button" onclick="myFunction()" name="button">CLICK ME</button>
  <div id="myDIV">Hello World</div>
  <script>
    function myFunction() {
      document.getElementById("myDIV").classList.toggle("animate__fadeIn");
    }
  </script>
</body>

</html>
Aakash
  • 21,375
  • 7
  • 100
  • 81
  • This div already occupied a space in the page. Thanks for trying. I got the solution. – jeno Mar 13 '21 at 06:23