Ad

Closable Div - Left Div Slides Close, While Right Div Width Expands

- 1 answer

I'm trying to make a div on the left side that can close by sliding to the left While the content in the right div expands to 100% filling the space of the closed div. image example

I found a demo here but its only for the closable div.

<div id="intro-wrap">
    <div class="open-intro">+</div>
    <div class="close-intro">-</div>

    <div id="contentWrap">
        <h1 class="main-header">Here is a Title</h1>
        <p class="main-desc">You can write whatever you want about yourself here. You can say you're a superhuman alien ant, arrived from the nether regions of Dwarf-Ant-Dom.</p>
    </div>
</div>
Ad

Answer

It has some work to do, but it'll get you started.

$(function() {
  "use strict";

  var isOpen = true;

  $('#open-close').on('click', function() {
    if (isOpen) {
      animate(false);
      isOpen = false;
    } else {
      animate(true);
      isOpen = true;
    }
  });


});

function animate(action) {

  if (action) {
    $('#left').animate({ width: "30vw" }, 600);
    $('#right').animate({width: "70vw",marginLeft: "-5px"}, 600);
  } else {
    $('#left').animate({width: "0px"}, 600);
    $('#right').animate({width: "99vw" }, 600);
  }

}
* {
  padding: 0;
  margin: 0;
}

#wrapper{
width: 100vw;
height: 100px;  
border: 2px solid purple;  
}
#wrapper > *{
  display: inline-block;
}
#left {
  position: relative;
  width: 30vw;
  height: 100px;
  background: green;
}
#right {
  position: relative;
  width: 65vw;
  height: 100px;
  background: navy;
}
#open-close {
  position: absolute;
  width: 10px;
  height: 10px;
  margin-top: 5px;
  margin-left: 5px;
  background: red;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="wrapper">

  <div id="left"></div>

  <div id="right">
    <div id="open-close"></div>
  </div>

</div>

Ad
source: stackoverflow.com
Ad