1

これが私の実際の例です:
http://jsfiddle.net/UGhKe/2/

CSS

#body {
    height: 200px;
    background: black;
    width: 100%;
}
.header {
    position: fixed;
    top: 0;
    background: #369;
    z-index: 1;
    width: 100%;
    height: 5em;
}
.content {
    position: absolute;
    top: 5em;
    overflow: hidden;
    height: 1000px;
    background: #936;
    z-index: 0;
    width: 100%;
}
.footer {
    position: fixed;
    bottom: 0;
    background: #396;
    width: 100%;
}
.large {
    font-size: 120%;
    padding: 2em;
}  

HTML

<div id="body">
    <div class="header">
        <div class="large">Header</div>
    </div>
    <div class="content">
        Content, you should be able to see this when you scroll to top.
    </div>
    <div class="footer">
        <div class="large">Footer</div>
    </div>
</div>  

上部をスクロールするとコンテンツがヘッダーの下に配置されるようにします(ただし、スクロールダウンするとヘッダーの下に非表示になります)-これは正常に機能します...

ただし、top: 5emを削除して、「ヘッダーの現在の高さを継承する」などを使用する必要があります-JSなしで可能ですか?

JS なしでは本当に不可能な場合は、JS を使用できますが、純粋な CSS で解決策を見つけたいと思います。

編集:

top: 5emを使用できない理由は、ヘッダーの高さが固定されていないためです。(ロゴ用の) 画像がテキスト内で使用され、max-width に設定されます。 100% にすると、iPhone の適切な幅に縮小され、iPad などではあまり拡大しません。

4

2 に答える 2

1

それがあなたのために働くかどうか見てください。http://jsfiddle.net/UGhKe/3/

div固定ヘッダーをシミュレートするために、同じheightが「固定されていない」別のものを追加しました。

HTML

<div id="body">
    <div id="blockHeader"></div>
    <div class="header">
        <div class="large">Header</div>
    </div>
    <div class="content">
        Content, you should be able to see this when you scroll to top.
    </div>
    <div class="footer">
        <div class="large">Footer</div>
    </div>
</div>

CSS

html, body { margin:0;  padding:0; }

#blockHeader 
{ 
    width:100%;
    height: 5em;
}

.content {
    position: absolute;
    overflow: hidden;
    height: 1000px;
    background: #936;
    z-index: 0;
    width: 100%;
}
于 2013-02-26T19:50:48.130 に答える
0

変数を使用してそれを行うことができます(そのためにはSASSまたはLESSを使用してください)。ペンを見てください。

コード:

$headerContentVariable: 5em;

#body {
    height: 200px;
    background: black;
    width: 100%;
}
.header {
    position: fixed;
    top: 0;
    background: #369;
    z-index: 1;
    width: 100%;
    height: $headerContentVariable;
}
.content {
    position: absolute;
    top: $headerContentVariable;
    overflow: hidden;
    height: 1000px;
    background: #936;
    z-index: 0;
    width: 100%;
}
.footer {
    position: fixed;
    bottom: 0;
    background: #396;
    width: 100%;
}
.large {
    font-size: 120%;
    padding: 2em;
}
于 2013-02-26T19:52:15.593 に答える