简体   繁体   English

我如何在容器div中有一个带有固定页眉和页脚的可滚动主体? 使用jQuery

[英]How can I have a scrollable body with fixed header and footer inside a container div? using jQuery

I have one container with two children elements inside of it. 我有一个容器,其中有两个子元素。 I want to place one child at the top and other child at the bottom of the container . 我想将一个孩子放在容器的顶部,另一个孩子放在容器的底部。 The middle part (the content) should scroll between the top and bottom child elements. 中间部分(内容)应在顶部和底部子元素之间滚动。

I want both children elements inside the container to have a fixed position and the content should scroll without scrolling both top and bottom div. 我希望容器内的两个子元素都具有固定的位置,并且内容应滚动而不滚动top和bottom div。

I'd like a solution using CSS or jQuery, please. 我想要一个使用CSS或jQuery的解决方案。 For a JSFiddle example, see my comment below. 对于JSFiddle示例,请参阅下面的评论。

I would like to add the below as a comment but because of low reputation I was unable to do it. 我想添加以下内容作为评论,但由于声誉低下,我无法做到这一点。

Take a look at the following jsfiddles: 看一下以下jsfiddles:

1) http://jsfiddle.net/davidpauljunior/g2ydV/8/ 1) http://jsfiddle.net/davidpauljunior/g2ydV/8/

2) http://jsfiddle.net/yASFU/ 2) http://jsfiddle.net/yASFU/

Style: 样式:

#content {
    width: 80%;
    margin: 0 auto;
    padding: 60px 0;
}

You could add another inner div to hold the content, and set overflow-y: scroll on it so that only the content will scroll and not the header and footer. 您可以添加另一个内部div来容纳内容,并设置overflow-y: scroll在其上overflow-y: scroll ,以便仅滚动内容,而不滚动页眉和页脚。 Here is a modified version of your fiddle to illustrate this concept: 这是您的小提琴的修改版本,用于说明此概念:

http://jsfiddle.net/jwnace/vtuv6wh6/ http://jsfiddle.net/jwnace/vtuv6wh6/

Just add position:fixed; 只需添加position:fixed; to the necessary elements in the CSS. 到CSS中的必要元素。 So the CSS should look something like: 因此CSS应该看起来像:

  .inside {
    top: 2px;
    border: 1px solid #000;
    background-color: #000;
   color: #FFF;
   height:50px;
   position:fixed;

  }

  .inside2 {
    bottom: 2px;
    border: 1px solid #000;
    background-color: #000;
   color: #FFF;
   height:50px;
   position:fixed;
  }

Further read Updated fiddle to reflect the same. 进一步阅读更新的小提琴以反映相同的意思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM