Reputation: 395
how can I make a <div>
element act like a background like the intro of this web jabarprov.go.id?
This is my codepen http://codepen.io/anon/pen/RGEmxB
$(document).ready(function(){
$(window).bind('scroll', function() {
var navHeight = $( window ).height() - 70;
if ($(window).scrollTop() > navHeight) {
$('nav').addClass('fixed');
}
else {
$('nav').removeClass('fixed');
}
});
});
#div1, #div2{
height:100vh;
}
#div1{background-color:cyan}
#div2{background-color:lightgrey}
nav {
position: absolute;
bottom: 0;
width: 100%;
background: Green;
padding:2px 0;
z-index:1;
}
.fixed {
position: fixed;
top: 0;
height: 70px;
z-index: 1;
}
section {
height: 100vh;
}
/* Screens Settings */
#screen1 {
background: #43b29d;
}
#screen1 p {
padding-top: 200px;
}
#screen2 {
background: #efc94d;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div id="div1" class="col-md-3">LEFT SECTION</div>
<div id="div2" class="col-md-9">RIGHT SECTION</div>
<nav class="main-bg">
<div class="container">
NAVIGATION HERE
</div>
</nav>
<section id="screen2"></section>
<section id="screen3"></section>
The problem is the intro my page is not fixed (acts like fixed background).
Upvotes: 1
Views: 76
Reputation: 232
I modified your code. Hope it is what you searched for http://codepen.io/anon/pen/qaLGrp
I made a wrapper around div1 and 2 which is fixed and made change to sizes
wrapper
<div id="intro-wrapper">
<div id="div1" class="col-md-3">a</div>
<div id="div2" class="col-md-9">b</div>
</div>
wrapper css and sizes
body{
padding-top: 100vh;
}
#intro-wrapper{
z-index: -1;
position: fixed;
top: 0;
width: 100vw;
}
#div1, #div2{
height:calc(100vh - 70px);
}
navigation position
nav {
position: absolute;
top: calc(100vh - 70px);
width: 100%;
background: Green;
padding:2px 0;
z-index:1;
height:70px;
}
Upvotes: 1