Bootstrap - 浮动div的固定顶部div不起作用

时间:2017-05-16 06:09:25

标签: javascript jquery html css twitter-bootstrap

我想让粘性标题保留在我的#main div的顶部。

这是我的js fiddle link。即使在响应模式下。我试过下面的代码,但它没有用。

@import "bootstrap-sprockets";
@import "bootstrap";
@import "font-awesome";
@import "navbar";
body,
html {
  height: 100%;
  overflow: hidden;
  position: relative;
}

body {
  padding: 10px;
}

.row-offcanvas {
  height: 100%;
  padding: 5% 0px 5% 0px;
}

#sidebar {
  width: inherit;
  min-width: 220px;
  max-width: 220px;
  background-color: black;
  float: left;
  height: 100%;
  position: relative;
  overflow: hidden;
}

#main {
  height: 200px;
  overflow-y: auto;
  overflow-x: hidden;
  background-color: white;
}

#sticky {
  background-color: green;
}


/*
 * off Canvas sidebar
 * --------------------------------------------------
 */

@media screen and (max-width: 768px) {
  .row-offcanvas {
    position: relative;
    -webkit-transition: all 0.25s ease-out;
    -moz-transition: all 0.25s ease-out;
    transition: all 0.25s ease-out;
    width: calc(100% + 230px);
    margin-top: 35px;
  }
  .visible-xs {
    padding-top: 10px;
  }
  .row-offcanvas-left {
    left: -230px;
  }
  .row-offcanvas-left.active {
    left: 0;
  }
  .sidebar-offcanvas {
    position: absolute;
    top: 0;
  }
}
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.css" rel="stylesheet" />
<div class="row-offcanvas row-offcanvas-left">
  <div id="sidebar" class="sidebar-offcanvas">
    <div class="col-md-12">
      <h2>
        Sidebar
      </h2>
    </div>
  </div>
  <div id="main">
    <div id="sticky">
      sticky header
    </div>
    <div class="col-md-12">
      <p class="visible-xs">
        <button type="button" class="btn btn-primary btn-xs" data-toggle="offcanvas"><i class="glyphicon glyphicon-chevron-left"></i></button>
      </p>
      <div class="row">
        <h2>
          main content
          <br/> main content
          <br/> main content
          <br/>main content
          <br/>main content
          <br/>main content
          <br/>main content
          <br/>main content
          <br/>
        </h2>
      </div>
    </div>
  </div>
</div>
<!--/row-offcanvas -->

有人能帮帮我吗?提前感谢您的帮助。 感谢

2 个答案:

答案 0 :(得分:0)

将以下CSS添加到id #sticky

position: fixed;
width: 100%;
z-index: 99;

答案 1 :(得分:0)

在调整输出窗口大小时 - 粘性标题宽度会发生变化。我想用主要内容制作那个。我的意思是粘性div必须与主div一起使用。