Floating Header: Scroll Effect using HTML,CSS,JQuery

Ever scrolled halfway down the page – and then the navbar at the top appears? This can be very useful and looks professional. This tutorial will introduce a very basic approach how to get this happening.

1. Download the basic ‘Bootstrap’  Pre-Config from ( BootStrap ) .

Now update the index.html with the following:

<!DOCTYPE HTML>

<html>

<head>

<link rel="stylesheet" href="css/bootstrap.css">

<script type="text/javascript" src="js/vendor/jquery-1.9.1.min.js"></script>

<script type="text/javascript" src="js/vendor/bootstrap.min.js"></script>

<style type="text/css">

body {

padding-top: 60px;

padding-bottom: 40px;

}

</style>

<script>

$(document).ready(function() {

var s = $("#s");

var pos = s.position();

//alert(pos.top);

$(window).scroll(function() {

var windowpos = $(window).scrollTop();

if(windowpos >= pos.top) {

$(".navbar").show();

} else if(pos.top >= windowpos) {

$(".navbar").hide();

}

});

});

</script>

</head>

<body>

<body>

<div class="navbar navbar-inverse navbar-fixed-top hide">

<div class="navbar-inner">

<div class="container">

<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button>

<a class="brand" href="#">Project name</a>

<div class="nav-collapse collapse">

<ul class="nav">

<li class="active"><a href="#">Home</a></li>

<li><a href="#about">About</a></li>

<li><a href="#contact">Contact</a></li>

<li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>

<ul class="dropdown-menu">

<li><a href="#">Action</a></li>

<li><a href="#">Another action</a></li>

<li><a href="#">Something else here</a></li>

<li class="divider"></li>

<li class="nav-header">Nav header</li>

<li><a href="#">Separated link</a></li>

<li><a href="#">One more separated link</a></li>

</ul>

</li>

</ul>

<form class="navbar-form pull-right">

<input class="span2" type="text" placeholder="Email">

<input class="span2" type="password" placeholder="Password">

<button type="submit" class="btn">Sign in</button>

</form>

</div>

<!--/.nav-collapse -->

</div>

</div>

</div>

<div class="container">

<!-- Main hero unit for a primary marketing message or call to action -->

<div class="hero-unit">

<h1>Hello, world!</h1>

<p>This is a template for a simple marketing or informational website. It includes a large callout called the hero unit and three supporting pieces of content. Use it as a starting point to create something more unique.</p>

<p><a href="#" class="btn btn-primary btn-large">Learn more &raquo;</a></p>

</div>

<!-- Example row of columns -->

<div class="row">

<div class="span4">

<h2>Heading</h2>

<p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>

<p><a class="btn" href="#">View details &raquo;</a></p>

</div>

<div class="span4">

<h2>Heading</h2>

<p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>

<p><a class="btn" href="#">View details &raquo;</a></p>

</div>

<div class="span4">

<h2>Heading</h2>

<p>Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.</p>

<p><a class="btn" href="#">View details &raquo;</a></p>

</div>

</div>

<div class='row' id="s">

<div class='span12'>

<div style='height:900px; background:#efefef;'> this would all be text </div>

</div>

</div>

<hr>

<footer>

<p>&copy; Company 2013</p>

</footer>

</div>

<!-- /container -->

</body>

</html>

and you’re done!

Leave a Reply