2012-02-19 3 views

답변

12

헤더는 CSS를 position:fixed 상단에 남아 ... 하나는 헤더 CSS를 설정할 수 있습니다 - 처음부터 바로 position:fixed 또는 그 페이지를 스크롤 시작하면 position:fixed로 변경 .. 및 내용에 헤더를 업데이트하면 그는 스크롤로 유지하려면 ...

// css 
.container { 
    height: 2000px; 
    width: 100%; 
    background-color: yellow; 
} 

.header { 
    text-align: center; 
    background-color: red; 
    height: 100px; 
    min-height: 50px; 
    width: 100%; 
} 

// js 

window.onscroll= function() { 
    var top = window.pageXOffset ? window.pageXOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop; 
    var header = document.getElementById("header"); 
    if (top > 50){ 
    header.style.position = "fixed"; 
    header.style.height = "50px"; 
    } else { 
    header.style.position = "relative"; 
    header.style.height = "100px"; 
    } 
} 

//html 
<div class="container"> 
    <div id="header" class="header"> 
    Hello World 
    </div> 
</div> 

데모 here

+0

Downvoting를 사용하여 많은 찾지 못했습니다. 요즘에는 순수한 CSS를 사용하여 이러한 기능을 만들 수 없습니다. js 도우미 마법을 사용해야합니다. – shabunc

+1

동의 .. dint가 사이트를 제대로 확인합니다 .. 그 이유는 부분적으로 정답이 정확합니다. 스크롤에서 JS를 사용하고 '위치 : 고정'을 지정하는 것이 맞을 것입니다. –

+0

@shabunc가 작업 피들로 답변을 업데이트했습니다. :) –

관련 문제