在固定位置叠加内滚动

6 html css twitter-bootstrap

JsBin.单击左侧的按钮.第一个是不需要滚动的叠加的示例(因此它完美地工作),第二个按钮什么都不做,第三个按钮是我正在描述的问题.请注意,我在css选项卡中的主样式之前复制/粘贴了ZenPen样式.

我正在使用Bootstrap 3.0和ZenPen.我的想法是,我在屏幕左侧有一个菜单,您可以单击按钮隐藏并显示不同的"应用程序"或叠加层.这些叠加正在使用position: fixed,但我无法为ZenPen叠加层执行此操作,否则我将无法在其中滚动.

如果我#wordInner { position: fixed;改为#wordInner { position: absolute;相反,它将能够滚动,但它不会占用整个页面(而是在正常container类的屏幕中间居中.在JsBin中,它已经打开position: absolute,但是更改它以查看我正在谈论的问题.

我的代码背后的逻辑是我使用一个col-lg-12和相同div的样式,以便它占用整个页面(因为width: 100%显然不起作用.)

主要css:

#wrap {
    position: relative;
}
#main {
    position: relative;
}
Run Code Online (Sandbox Code Playgroud)

ZenPen的css(默认ZenPen css的链接):

#word {
    overflow: hidden;
    text-shadow: none;
}

#word b, #word strong {
    color: #000;
}

#wordInner {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;

    padding: 5px;
    /* because nav is about 100 px, zenpen's icon menu is about 65px */
    padding-left: 165px;
}

#word {
    overflow-y: scroll;

-webkit-transition: all 600ms;
-moz-transition: all 600ms;
-ms-transition: all 600ms;
-o-transition: all 600ms;
    transition: all 600ms;
}

#word section {
    height: 100%;
    margin: auto;
}
Run Code Online (Sandbox Code Playgroud)

和相关的HTML(链接到ZenPen html:ZenPen html):

 <div class="container" id="wrap">
      <div class="container" id="main">
      <!-- snip irrelevant html -->
      </div>
  </div>

<div class="container" id="apps">
    <div class="row">
       <div class="col-lg-12"  id="appsInner">
         <div><a href="#" id="close_apps">Close</a></div>
         <input type="text" class="form-control" placeholder="Search here" />
       </div>
    </div>
</div>

  <div class="container" id="word">
    <div class="row">
    <div class="col-lg-12 yin" id="wordInner" >
    <div><a href="#" id="close_word">Close</a></div>
    <!-- snip zenpen html -->
    </div>
    </div>
    </div>
  </div>
  </div>

    <nav>
    <ul>
        <li><a href="#" class="btn light"  id="open_apps"><span class="entypo-search"></span></a><span class='button-text'>Apps</span></li>
        <li><a href="#" class="btn red"  id="home"><span class="entypo-home"></span></a><span class='button-text'>Home</span></li>
        <li><a href="#" class="btn green"  id="open_word"><span class="entypo-leaf"></span></a><span class='button-text'>Word</span></li>
    </ul>
  </nav>
Run Code Online (Sandbox Code Playgroud)

Mat*_*tra 13

您可以将溢出设置html, bodyoverflow: hidden,然后设置#wordInner { position: fixed; overflow: auto;.这将确保页面右侧只有一个可见的滚动条,并且仅在需要时.如果你需要overflow设置visible为其他页面,你可以在打开和关闭#word应用程序时使用jQuery设置它(因为你已经在使用它了).

选项1(仅使用CSS): jsBin

html, body
{
  overflow: hidden;
}

#wordInner {  
  position: fixed;
  overflow: auto;
  ...
}
Run Code Online (Sandbox Code Playgroud)

选项2(使用CSS和jQuery): jsBin

CSS

#wordInner {  
  position: fixed;
  overflow: auto;
  ...
}
Run Code Online (Sandbox Code Playgroud)

jQuery的

$("#open_word").click(function(event) {
  event.preventDefault();   
  $("html, body").css("overflow", "hidden");
  $("#word").show();
});

$("#close_word").click(function(event) {
  event.preventDefault();
  $("html, body").css("overflow", "visible");
  $("#word").hide();
});
Run Code Online (Sandbox Code Playgroud)