I have made a simple Parallax image and Sticky header on Scroll with Html Css & Js and everything works fine except the main div which contains an h1 tag (as you can see in my code below) that does collapeses under the navigation menu!
Here's my code: You should use a random image to get a better result to test the code.
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"></meta>
<title>Daygostar Homepage</title>
<link rel="stylesheet" type="text/css" href="css/styles.css"/>
</head>
<body>
<div id="wrapper">
<div id="header">
<img src="images/bg.png" id="bg" alt="Background Image"/>
</div>
<div id="content">
<div id="navbar">
<div id="logo">
Daygostar
</div>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Blogs</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</div>
<div id="main">
<h1>Daygostar Web Design</h1>
</div>
</div>
<script type="text/javascript" src="js/script.js"></script>
</body>
</html>
CSS:
*{padding:0;margin:0;font-family:arial;}
#header{
height:91vh;
}
#header > img{
width:100%;
height:91vh;
position:fixed;
z-index:1;
}
#content{
height:100vh;
z-index:10;
background-color:#fff;
position:relative;
}
#content > #navbar{
position:absolute;
top:0;
width:100%;
height:80px;
background-color:#000;
}
#main{
height:100vh;
border:solid blue;
background-color:#fff;
position:relative;
padding-top:200px;
text-align:center;
font-size:25px;
z-index:10;
}
#main > *{
margin-top:30px;
}
#logo{
color:#fff;
font-size:35px;
float:left;
margin-top:15px;
margin-left:20px;
cursor:pointer;
}
#navbar > ul{
float:right;
width:700px;
list-style:none;
margin-top:25px;
}
#navbar > ul > li{
display:inline;
}
#navbar ul > li > a{
color:#fff;
font-size:20px;
text-decoration:none;
padding:10px 30px;
}
#navbar ul > li > a:hover{
text-decoration:underline;
}
Javascript:
var header = document.getElementById("header");
var navBar = document.getElementById("navbar");
var bg = document.getElementById("bg");
var navbarHeight = navBar.offsetHeight;
var headerHeight = header.offsetHeight;
header.style.height = screen.height-navbarHeight;
function initParallex(){
if (window.pageYOffset > headerHeight){
navBar.style.position = "fixed";
navBar.style.top = "0";
}else{
navBar.style.position = "absolute";
navBar.style.top = "0";
}
bg.style.top = -(window.pageYOffset/5)+"px";
}
window.addEventListener("scroll", initParallex);
As you can see I have alreay added an if/else statements to check wheather the headerHeight is less than window.pageYOffset or not in script.js file but as soon as the web page reaches the main div ,the collapsing nav would hide!
It would be awesome if you know how to solve this problem...