【问题标题】:How to set div scrollable when content more than size of the page?当内容超过页面大小时如何设置div可滚动?
【发布时间】:2013-10-17 03:27:42
【问题描述】:

我有下一页:

<div id = "menu">
    Menu on the left side
</div>
<div id = "header">
    Header content of the page
</div>
<div id = "body">
    Data Data Data Data Data Data Data 
</div>
<div id = "footer">
    Additional Information
</div>

下一个布局:菜单应该在左侧:

#menu{
    background: #244a7c;
    padding: 7px 23px 0 7px;
    width: 299px;
    height: 1000px;
    overflow: inherit;
    margin-left: 0px;
    display: block;
    float: left;
}
#header{
    display: inline-block;
    width: 400px;
    border-bottom: 1px solid rgb(238, 238, 238);
}

Body 里面可以有不同的数据。我的问题是:

当正文内容超过用户页面时,我想修复除正文之外的所有div。菜单应该在左侧,页眉应该在页面顶部,页脚在底部,只有正文应该是可滚动的。

请帮忙。

谢谢!

【问题讨论】:

  • 你能把代码放在jsfiddle.net吗?
  • 您想要这样的布局 12。你需要什么浏览器支持?
  • 我需要一个像 1 这样的布局,但我还需要一个动态的布局。如果内容小于用户页面大小,则效果很好,但如果内容大于页面大小,那么我只需要正文可滚动。

标签: javascript jquery html asp.net css


【解决方案1】:

这里有 2 个纯 CSS 解决方案

不固定任何高度(页眉/页脚)或宽度(左栏)。

我实际上更喜欢第二种解决方案。 (即使他的浏览器支持较少)

1 - 使用 CSS 技巧

这是一个完全响应式的设计,适用于所有浏览器(IE10、FF、Chrome、Safari、Opera、移动浏览器)

Working Fiddle

HTML:

<div class="Container">
    <div class="Header">
    </div>
    <div class="HeightTaker">
        <div class="Wrapper Container Inverse">
            <div>
                <div class="Footer">
                </div>
            </div>
            <div class="HeightTaker">
                <div class="Wrapper">
                    <div class="LeftMenu">
                    </div>
                    <div class="Content">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

CSS:

*
{
    margin: 0;
    padding: 0;
}
html, body, .Container
{
    height: 100%;
}
    .Container:before
    {
        content: '';
        height: 100%;
        float: left;
    }
.HeightTaker
{
    position: relative;
    z-index: 1;
}
    .HeightTaker:after
    {
        content: '';
        clear: both;
        display: block;
    }
.Wrapper
{
    position: absolute;
    width: 100%;
    height: 100%;
}
.Inverse, .Inverse > *
{
    -moz-transform: rotateX(180deg);
    -ms-transform: rotateX(180deg);
    -o-transform: rotate(180deg);
    -webkit-transform: rotateX(180deg);
    transform: rotateX(180deg);
}
.LeftMenu
{
    height: 100%;
    float: left;
}
.Content
{
    overflow: auto;
    height: 100%;
}

/*For demonstration only*/
p
{    
    font-size: 1.3em;
}

.Important
{
    font-weight: bolder;
    color: white;
}

body > .Container
{
    text-align: center;
}

.Header
{
    background-color: #bf5b5b;
}
.LeftMenu
{
    background-color:  #bdbe4c;
}

.Content
{
    background-color: #90adc1;
}
.Footer
{
    background-color: #b5a8b7;
}

2 - 使用 Flex

这种布局也可以使用flex来实现,但是目前浏览器的支持是纯的。 这是Working Fiddle 仅限 FF、Chrome、IE10。

HTML:(更简单)

<header>
</header>
<section class="Middle">
    <div class="LeftMenu">
    </div>
    <div class="Content">
    </div>
</section>
<footer>
</footer>

CSS:

*
{
    margin: 0;
    padding: 0;
}
html, body
{
    height: 100%;
    text-align: center;
}

body
{
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;

    -webkit-flex-direction: column;
    -ms-flex-direction: column;
    flex-direction: column;
}

.Middle
{    
    -webkit-flex: 1 1 auto;
    -ms-flex: 1 1 auto;
    flex: 1 1 0;

    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;

    overflow: hidden;
}

.Content
{   
    -webkit-flex: 1 1 auto;
    -ms-flex: 1 1 auto;
    flex: 1 0 0;

    overflow: auto;
}

/*For demonstration only*/
p
{    
    font-size: 1.3em;
}

.Important
{
    font-weight: bolder;
    color: white;
}

header
{
    background-color: #bf5b5b;
}
.LeftMenu
{
    background-color:  #bdbe4c;
}

.Content
{
    background-color: #90adc1;
}
footer
{
    background-color: #b5a8b7;
}

【讨论】:

    【解决方案2】:

    如果您将headerfootermenu 位置设置为固定并保持主体不变,它应该可以工作。只有正文可以滚动。

    #header { 
        position: fixed;
        top: 0; 
        display: inline-block;
        width: 400px;
        border-bottom: 1px solid rgb(238, 238, 238);
    }
    #footer { 
        position: fixed;
        bottom: 0; 
    }
    #menu { 
        position: fixed; 
        left: 0; 
        background: #244a7c;
        padding: 7px 23px 0 7px;
        width: 299px;
        height: 1000px;
    }
    #body { 
        margin-left: 300px;
        margin-top: <header-height>;
        margin-bottom: <footer-height>;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-31
      • 1970-01-01
      • 2011-09-25
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      相关资源
      最近更新 更多