Forum Moderators: open
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>fixed header on scroll</title>
<style type="text/css">
body {
margin:0;
background-color:#f0f0f0;
}
#container {
width:960px;
height:2000px; /* this value is only here to create scrolling */
padding:50px 20px 20px;
border:1px solid #999;
margin:auto;
background-color:#fff;
box-shadow: 0 0 8px #333;
}
#header {
position:fixed;
width:960px;
height:30px;
padding:10px 20px;
margin:-109px 0 0 -20px;
border-bottom:1px solid #987;
background-color:#fed;
box-shadow: 0 0 8px #666;
font-weight:bold;
color:#654;
text-align:center;
}
</style>
<script type="text/javascript">
function init(){
c=-109; /* this value is equal to the total height of the
#header - (height, padding, border and box-shadow) */
speed=40; /* adjust this value to suit */
test=true;
onscroll=function(){
if(test==true){
godown();
test=false;
}
}
}
function godown(){
document.getElementById('header').style.marginTop=c+'px';
c++;
if(c>=-50) { /* this value is equal to the #container padding top */
clearTimeout(st);
c=-50;
return;
}
else {
st=setTimeout(function(){godown();},speed);
}
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
</script>
</head>
<body>
<div id="container">
<div id="header">This is the fixed header</div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec
ultricies sollicitudin nisi, ut molestie felis adipiscing sit
amet. Sed auctor vehicula commodo. Nam a nibh neque, vitae
faucibus felis. Vivamus at metus eget eros consequat fringilla.
Quisque magna magna, laoreet eu tempus sit amet, fringilla at
tellus. Praesent felis tortor, scelerisque vitae fringilla
non, porttitor et lacus. Ut ut sapien nec quam tincidunt
consectetur in nec lacus.
</p>
</div><!-- end #container -->
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>fixed header on scroll</title>
<style type="text/css">
body {
margin:0;
background-color:#f0f0f0;
}
#container {
width:960px;
height:2000px; /* this value is only here to create scrolling */
padding:50px 20px 20px;
border:1px solid #999;
margin:auto;
background-color:#fff;
box-shadow: 0 0 8px #333;
}
#header {
position:fixed;
width:960px;
height:30px;
padding:10px 20px;
margin:-109px 0 0 -20px;
border-bottom:1px solid #987;
background-color:#fed;
box-shadow: 0 0 8px #666;
font-weight:bold;
color:#654;
text-align:center;
}
</style>
<script type="text/javascript">
function init(){
c=-109; /* this value is equal to the
#header margin-top value) */
speed=40; /* adjust this value to suit */
test=true;
num=1;
onscroll=function(){
window.scrollY?
sy=window.scrollY:
sy=document.body.parentNode.scrollTop;
if(test==true){
godown();
test=false;
}
if((test==false)&&(sy==0)){
num=-1
godown();
}
}
}
function godown(){
document.getElementById('header').style.marginTop=c+'px';
c+=num;
if(c<=-109){
clearTimeout(st);
num=1
test=true;
return;
}
else {
if(c>=-50) { /* this value is equal to the
#container padding-top value */
clearTimeout(st);
return;
}
else {
st=setTimeout(function(){godown();},speed);
}
}
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
</script>
</head>
<body>
<div id="container">
<div id="header">This is the fixed header</div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec
ultricies sollicitudin nisi, ut molestie felis adipiscing sit
amet. Sed auctor vehicula commodo. Nam a nibh neque, vitae
faucibus felis. Vivamus at metus eget eros consequat fringilla.
Quisque magna magna, laoreet eu tempus sit amet, fringilla at
tellus. Praesent felis tortor, scelerisque vitae fringilla
non, porttitor et lacus. Ut ut sapien nec quam tincidunt
consectetur in nec lacus.
</p>
</div><!-- end #container -->
</body>
</html>
No problem, you're very welcome. ;)