Closable div - 左侧div滑动关闭,右侧div宽度扩展

时间:2016-01-02 00:31:41

标签: javascript css html5

我正试图在左侧制作一个div,可以通过向左滑动关闭而右边div中的内容扩展到100%填充封闭div的空间。 image example

我在这里找到了demo,但它只用于可关闭的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>

1 个答案:

答案 0 :(得分:0)

它有一些工作要做,但它会让你开始。

$(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>