两个div独立滚动

时间:2015-07-27 08:09:09

标签: html css scroll


我需要帮助才能使这两个<div>#side-nav#content-wrapper)独立滚动,

HTML:

<div id="wrapper">
    <div id="top-nav">
        Top nav
    </div>
    <div id="side-nav">
        <ul>
            <li>Thing</li>
            <li>Thing</li>
        </ul>
    </div>
    <div id="content-wrapper">
        <!-- Ton of conent here -->
    </div>
</div>

CSS:

#wrapper {
  width: 100%;
  background-color: #fff;
}

#top-nav {
  position: fixed;
  top: 0;
  height: 60px;
  width: 100%;
  background-color: green;
}

#side-nav {
  position: fixed;
  width: 250px;
  height:100vh;
  overflow-y: scroll;
  background-color: red;
}

#content-wrapper {
  margin: 60px 0 0 250px;
  padding: 0 30px;
  overflow-y: scroll;
  background-color: blue;
}

现在如果我将#side-nav滚动到结尾或顶部,#content-wrapper也会滚动。即使没有那么多#side-nav<li>也必须保持整页高度并固定。

我很快在这里写了笔:

http://codepen.io/blizqery/pen/QbZzRN

谢谢!

2 个答案:

答案 0 :(得分:9)

请检查:http://codepen.io/anon/pen/xGyMjM

您需要将height设置为content-wrapper,并设置left,right和amp;顶部。

#side-nav {
  position: fixed;
  width: 250px;
  height:100vh;
  left: 0;
  right: 0;
  overflow-y: scroll;
  background-color: red;
  top: 60px;
}

#content-wrapper {
  margin: 60px 0 0 250px;
  padding: 0 30px;
  overflow-y: scroll;
  position: fixed;
  left: 0;
  top: 0;
  height:100vh;
  background-color: blue;
}

答案 1 :(得分:0)

我相信这适合您的问题

   body{
      margin:0px;
    }
    #top-nav {
      position: fixed;
      top: 0;
      height: 10vh;
      width: 100%;
      background-color: green;
    }
    #wrapper {
      width: 100%;
      background-color: #fff;
    }

    #side-nav {
      float:left;
      width: 250px;
      height: 90vh;
      overflow-y: scroll;
      background-color: red;
    }

    #content-wrapper {
      margin: 10vh 0 0 250px;
      padding: 0 30px;
      overflow-y: scroll;
      background-color: blue;
      height:90vh;
    }