Created
March 28, 2015 02:29
-
-
Save mpetroff/4666657beeb85754611f to your computer and use it in GitHub Desktop.
Bootstrap Navbar without jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> | |
<title>Navbar Template for Bootstrap</title> | |
<!-- Bootstrap core CSS --> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> | |
<!-- Custom styles for this template --> | |
<style> | |
body { | |
padding-top: 20px; | |
padding-bottom: 20px; | |
} | |
.navbar { | |
margin-bottom: 20px; | |
} | |
</style> | |
<!-- Styles for avoiding jQuery --> | |
<link rel="stylesheet" href="navbar.css"> | |
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> | |
<!--[if lt IE 9]> | |
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> | |
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> | |
<![endif]--> | |
</head> | |
<body> | |
<div class="container"> | |
<!-- Static navbar --> | |
<nav class="navbar navbar-default"> | |
<div class="container-fluid"> | |
<div class="navbar-header"> | |
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> | |
<span class="sr-only">Toggle navigation</span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
</button> | |
<a class="navbar-brand" href="#">Project name</a> | |
</div> | |
<div id="navbar" class="navbar-collapse collapse"> | |
<ul class="nav navbar-nav"> | |
<li class="active"><a href="#">Home</a></li> | |
<li><a href="#">About</a></li> | |
<li><a href="#">Contact</a></li> | |
<li class="dropdown"> | |
<div href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Dropdown <span class="caret"></span></div> | |
<ul class="dropdown-menu" role="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="dropdown-header">Nav header</li> | |
<li><a href="#">Separated link</a></li> | |
<li><a href="#">One more separated link</a></li> | |
</ul> | |
</li> | |
</ul> | |
</div><!--/.nav-collapse --> | |
</div><!--/.container-fluid --> | |
</nav> | |
<!-- Main component for a primary marketing message or call to action --> | |
<div class="jumbotron"> | |
<h1>Navbar example</h1> | |
<p>This is adapted from the default Bootstrap navbar example to work without JavaScript on larger devices and work without jQuery on smaller devices (but with JavaScript).</p> | |
<p> | |
<a class="btn btn-lg btn-primary" href="//mpetroff.net/2015/03/bootstrap-navbar-without-jquery/" role="button">View blog post about this »</a> | |
</p> | |
</div> | |
</div> <!-- /container --> | |
<!-- Navbar JavaScript --> | |
<script src="navbar.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your navbar.js in 5 lines.
The bootstrap framework automatically expands and shrinks the navbar according to the user screen width, no need to perform explicit checks and close it manually, neither we need to add and remove classes to do that.
The only thing we care is that when the navbar becomes too crammed and replaced by the 'hambuger' button, to provide a way for the user to invoke the dropdown menu. When the screen width becomes large enough, the 'hambuger' button will be replaced by the bootstrap framework with the full navbar width.