Two fixed scrollable left sidebars

时间:2015-07-28 16:10:39

标签: html css

I want to create two fixed scrollable left sidebars. How can i make both of them be scrollable independent of each other?

The following code isn't working.

.category {
  position: fixed;
  height: 250px;
  overflow-y: scroll;
  overflow-x: hidden;
  z-index: 1;
}
.sub-category {
  position: fixed;
  top: 250px;
  overflow-y: scroll;
  overflow-x: hidden;
  z-index: 1;
}
<div class="small-2 columns category">
  <ul>
    some list elements
  </ul>
</div>

<div class="small-2 columns sub-category">
  <ul>
    other list elements
  </ul>
</div>

3 个答案:

答案 0 :(得分:1)

Is this what you are looking for? https://jsfiddle.net/4654eaue/13/

<div class="small-2 columns category">
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
    </div>

    <div class="small-2 columns sub-category">
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
        <ul>
          some list elements          
        </ul>
</div>

I did nothing but add content....

答案 1 :(得分:0)

Well, a few things you should note :

  • ul needs to have a li tag as a containing element.
  • If the two adjacent elements have fixed positioning, they would overlay, hence you need to position them accordingly. See the left propoerty I have added for a better understanding.

ul {
    margin: 0;
    padding: 10px;
}
ul li {
    list-style-type: none;
}
.category {
    background-color: #bbb;
    position: fixed;
    display:inline-block;
    height: 250px;
    overflow-y:scroll;
    overflow-x:hidden;
    z-index: 1;
}
.sub-category {
    background-color: #eee;
    position: fixed;
    display: inline-block;
    height: 250px;
    overflow-y:scroll;
    overflow-x:hidden;
    z-index:1;
    left: 200px;
}
<div class="small-2 columns category">
    <ul>
        <li>some list elements</li>
        <li>some list elements</li>
        <li>some list elements</li>
        <li>some list elements</li>
        <li>some list elements</li>
        <li>some list elements</li>
        <li>some list elements</li>
        <li>some list elements</li>
        <li>some list elements</li>
        <li>some list elements</li>
        <li>some list elements</li>
        <li>some list elements</li>
        <li>some list elements</li>
    </ul>
</div>
<div class="small-2 columns sub-category">
    <ul>
        <li>child list elements</li>
        <li>child list elements</li>
        <li>child list elements</li>
        <li>child list elements</li>
        <li>child list elements</li>
        <li>child list elements</li>
        <li>child list elements</li>
        <li>child list elements</li>
        <li>child list elements</li>
        <li>child list elements</li>
        <li>child list elements</li>
        <li>child list elements</li>
    </ul>
</div>

答案 2 :(得分:0)

display inline-block and vertical-align top

body {
  height: 350px;
  overflow:hidden;
}
#category {
  position: relative;
  vertical-align: top;
  height: 350px;
  left: 0;
  overflow-y: scroll;
  overflow-x: hidden;
  z-index: 1;
}
#sub-category {
  position: relative;
  vertical-align: top;
  height: 350px;
  overflow-y: scroll;
  overflow-x: hidden;
  z-index: 1;
}
div {
  display: inline-block;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>

<body>
  <div id="category">
    <ul>
      <li>Coffee</li>
      <li>Tea</li>
      <li>Milk</li>
      <li>Coffee</li>
      <li>Tea</li>
      <li>Milk</li>
      <li>Coffee</li>
      <li>Tea</li>
      <li>Milk</li>
      <li>Coffee</li>
      <li>Tea</li>
      <li>Milk</li>
      <li>Coffee</li>
      <li>Tea</li>
      <li>Milk</li>
      <li>Coffee</li>
      <li>Tea</li>
      <li>Milk</li>
    </ul>
  </div>
  <div id="sub-category">
    <ul>
      <li>Coffee</li>
      <li>Tea</li>
      <li>Milk</li>
      <li>Coffee</li>
      <li>Tea</li>
      <li>Milk</li>
      <li>Coffee</li>
      <li>Tea</li>
      <li>Milk</li>
      <li>Coffee</li>
      <li>Tea</li>
      <li>Milk</li>
      <li>Coffee</li>
      <li>Tea</li>
      <li>Milk</li>
      <li>Coffee</li>
      <li>Tea</li>
      <li>Milk</li>
    </ul>
  </div>
</body>

</html>