1

I'm trying to get a div to expand from the top of the page to the bottom. When users click button then animation starts the div will be hidden (height 0%), till it fills its content. I tried to do it like so, but none of it seems to be working.

CSS and HTML

<div class="container">
    <h1>Lorem Ipsum</h1>
    <h1>Lorem Ipsum</h1>
    <h1>Lorem Ipsum</h1>
    <h1>Lorem Ipsum</h1>
    <h1>Lorem Ipsum</h1>
    <h1>Lorem Ipsum</h1>
    <h1>Lorem Ipsum</h1>
    <h1>Lorem Ipsum</h1>
</div>    

<button type="button" id="expand">Expand</button>

.container{
    background:lightblue;
    top: 0px; 
    left: 0px; 
    width: 100%; 
    display: block; 
    height:0px;
    overflow:hidden;
 }

http://jsfiddle.net/3hmvy278/

halfer
  • 19,824
  • 17
  • 99
  • 186
AbhiRam
  • 2,033
  • 7
  • 41
  • 94
  • Post JS code too, also animation that you going to use – Mehdi Dehghani Mar 14 '19 at 05:00
  • i am learner i did not write any code in js please guide me to do that – AbhiRam Mar 14 '19 at 05:01
  • post your effort code here. – Sarabjit Singh Mar 14 '19 at 05:03
  • I think SO is not teaching class and you can not use SO for doing your home works. you should do it yourself – Mehdi Dehghani Mar 14 '19 at 05:07
  • 1
    Possible duplicate of [How can I transition height: 0; to height: auto; using CSS?](https://stackoverflow.com/questions/3508605/how-can-i-transition-height-0-to-height-auto-using-css) – Mehdi Dehghani Mar 14 '19 at 05:09
  • You need to use `max-height` with CSS transition. You would find detailed answer in link suggested by @Mehdi Dehghani. CSS transition will work only if you know maximum possible height your content will ever have. Otherwise, you will need to use some javascript method to expand/collapse. If you are using `jQuery`, you can do it with simple `$('.elementClass').toggle()`. – Rhythm Ruparelia Mar 14 '19 at 06:12

2 Answers2

1

Give this a try. I'm using no dependencies, only some vanilla JavaScript and a CSS transition.

document.querySelector(".myButton").addEventListener("click", () => {
  document.querySelector(".container").classList.toggle("start");
});
.container {
  background: lightblue;
  width: 100%;
  height: 0;
  overflow: hidden;
  transition: 1s height ease-in-out;
}

.container.start {
  height: 100vh;
}

/* Ignore */

body {
  margin: 0;
}

.myButton {
  position: absolute;
  top: 0;
  left: 0;
  font-size: 100%;
}
<div class="container">
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
</div>

<button class="myButton">Toggle</button>
Andy Hoffman
  • 18,436
  • 4
  • 42
  • 61
0

Here i am adding class .height on click of button.

.height {
  height: 100%
}

$(document).ready(function() {
  $("button").click(function() {
    $(".container").toggleClass("height")
  });
});
.container {
  background: lightblue;
  top: 0px;
  left: 0px;
  width: 100%;
  display: block;
  height: 0px;
  overflow: hidden;
}

.height {
  height: 100%
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
</div>

<button type="button" id="expand">Expand</button>

Here I am toggling the display of div on click of button

$(document).ready(function() {
  $("button").click(function() {
    $(".container").toggle()
  });
});
.container {
  background: lightblue;
  top: 0px;
  left: 0px;
  width: 100%;
  display: block;
  height: 100%;
  overflow: hidden;
  display:none;
}

.height {
  height: 100%
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
  <h1>Lorem Ipsum</h1>
</div>

<button type="button" id="expand">Expand</button>
Xenio Gracias
  • 2,728
  • 1
  • 9
  • 16