use jquery to build navbar

use jquery to build navbar

example1

https://codepen.io/jmkulakowski/pen/JXMmVx use bootstrap and jquery

js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Add some space at the top to accomodate fixed menu
accountForFixedMenu(0.5);
// Enable smooth scroll when navigating to various sections
// Change nav from transparent to white on scroll
navDynamicStyles(100);
// Add fade effect on page scroll to feature image and highlight main text
fadeFeatureImgOnScroll(100, 1);
/*-- Parallax Effect --*/
// Target the background element to which effect will be applied
var jumbotron = document.querySelector('.jumbotron');
var container = jumbotron.children[0];
var parallaxBg = container.children[0];
function parallax(){
var scrolltop = window.pageYOffset; // get number of pixels document has scrolled vertically
parallaxBg.style.top = scrolltop * 0.2 + 'px'; // move parallaxBg at 20% of scroll rate
}
// Attach the effect to the window scroll
window.addEventListener('scroll', function(){ // on page scroll
requestAnimationFrame(parallax); // call parallax() on next available screen paint
}, false)
/** -----------------------------------------------------------------
* Function Definitions
-------------------------------------------------------------------*/
// Add an event listener to any element
/*
* When calling this function, make sure to enclose the element and listner
* tags in quotes - i.e. addListener(myElement, "onload", doThisFunction)
*/
function addListener(elementTag, listener, func) {
var element = document.querySelectorAll(elementTag);
for (i = 0; i < element.length; i++) {
element[i].addEventListener(listener, func, false);
}
}
function accountForFixedMenu(factor) { // factor from 0 - 1 (% to offset)
var jumbotron = document.querySelector(".jumbotron");
var navbar = document.querySelector(".navbar");
// Add navbar height + percentage of jumbotron' bottom padding
var targetTopPadding = Number(navbar.offsetHeight) +
Number($(jumbotron).css("padding-bottom").replace("px", "") * factor);
// Set top padding of jumbotron
jumbotron.style.paddingTop = targetTopPadding + "px";
}
function navDynamicStyles(buffer) {
$(window).scroll(function(i) {
var scrollDistance = $(window).scrollTop(); // How far window id from top
// Reference the navbar elements
var navbar = jQuery('.navbar');
var brandImage = jQuery('.navbar-brand > img');
var siteTitle = jQuery('.navbar-brand p');
var navLinks = jQuery('.navbar-default .navbar-nav>li>a');
var navDropMenu = jQuery('.navbar-default .navbar-collapse, .navbar-default .navbar-form');
/* -----------------------------
-- Scrolled Nav Settings --
------------------------------*/
// If the window is scrolled beyond the buffer
if (scrollDistance > buffer) {
navbar.css({
backgroundColor: "#fff",
background: "linear-gradient(#fff, #f8f8f8)",
background: "-webkit-linear-gradient(#fff, #f8f8f8)",
background: "-o-linear-gradient(#fff, #f8f8f8)",
background: "-moz-linear-gradient(#fff, #f8f8f8)"
});
brandImage.css({
background: "none",
boxShadow: "none"
});
siteTitle.css({
color: "#777",
padding:"10px"
});
siteTitle.hover(
function() {
jQuery(this).css("color", "#aaa");
},
function() {
jQuery(this).css("color", "#777");
});
navLinks.css({
color: "#777",
padding:"20px"
});
navLinks.hover(
function() {
jQuery(this).css("color", "#aaa");
},
function() {
jQuery(this).css("color", "#777");
});
navDropMenu.css({
borderColor: "#e7e7e7"
});
} // END if
/* -----------------------------
-- Transparent Nav Settings --
------------------------------*/
// If the window is scrolled to the top of the page
else if (scrollDistance < buffer) {
navbar.css({
background: "none"
});
brandImage.css({
backgroundColor: "#ccc",
boxShadow: "0 0 3px #ccc"
});
siteTitle.css({
color: "#ccc",
padding:"20px"
});
siteTitle.hover(
function() {
jQuery(this).css("color", "#eee");
},
function() {
jQuery(this).css("color", "#ccc");
});
navLinks.css({
color: "#ccc",
padding:"30px"
});
navLinks.hover(
function() {
jQuery(this).css("color", "#eee");
},
function() {
jQuery(this).css("color", "#ccc");
});
navDropMenu.css({
borderColor: "#444"
});
} // END else if
});
}
function fadeFeatureImgOnScroll(scrollDistance, parallaxFactor) {
$(window).scroll(function(i) {
// Scroll distance from window top
var scrollVar = $(window).scrollTop();
// The featured image
var profileImage = $('.jumbotron .container').children('.profile-img');
// The heading Text
var headText = $('.jumbotron .container h1');
var subHeadText = $('.jumbotron .container h4');
profileImage.css({
'top': parallaxFactor * scrollVar,
'opacity': (scrollDistance - scrollVar) / 100
});
// Add soft transition to head text
headText.addClass('soft-transition');
// If the window is scrolled beyond the scrollDistance
// and featured image is invisible
if (scrollVar > scrollDistance) {
// Add a text hightlight to the head text
headText.css({
textShadow: "0 0 10px #ccc"
});
subHeadText.css({
textShadow: "0 0 10px #ccc"
});
} else {
// Otherwise, remove the effect
headText.css({
textShadow: "none"
});
subHeadText.css({
textShadow: "none"
});
}
});
}

css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
.soft-transition {
transition: all 0.25s ease-out;
-webkit-transition: all 0.25s ease-out;
-moz-transition: all 0.25s ease-out;
}
.container {
max-width: 1024px;
}
.container-fluid {
margin: 0;
padding: 0;
}
/*-- Navbar settings --*/
.navbar {
transition: all 0.5s ease-out;
}
.navbar {
background: none;
border: none;
}
.navbar-brand {
padding: 10px; /* Allows more space for brand logo to expand */
}
.navbar-brand > img {
max-height: 100%;
display: inline-block;
margin-right: 2px;
background-color: #ccc;
box-shadow: 0 0 3px #ccc;
}
.navbar-brand p {
display: inline-block;
color: #ccc;
padding:20px;
}
.navbar-brand p:hover {
color: #eee;
}
.navbar-default .navbar-nav>li>a {
color: #ccc;
padding:30px;
}
.navbar-default .navbar-nav>li>a:hover {
color: #eee;
}
.navbar-default .navbar-collapse, .navbar-default .navbar-form {
border-color: #444;
}
/*-- Mobile nav --*/
.navbar-default .navbar-toggle, .navbar-default .navbar-toggle:focus, .navbar-default .navbar-toggle:hover {
background: none;
border: none;
}
/*-- Jumbotron settings --*/
.jumbotron {
position: relative;
margin: 0;
text-align: center;
color: #fff;
background-color: #333;
overflow: hidden;
}
.jumbotron .container .profile-img, .jumbotron .container h1, .jumbotron .container h4, .jumbotron .container hr, .jumbotron .container a {
position: relative;
}
.jumbotron .parallax-img {
width: 100%;
position: absolute;
top: 0;
left: 0;
z-index: 0;
}
.jumbotron .profile-img {
width: 25%;
margin: 20px 0;
border-radius: 100%;
border: 8px solid #eee;
-moz-box-shadow: 0px 0px 20px #ccc;
-webkit-box-shadow: 0px 0px 20px #ccc;
box-shadow: 0px 0px 20px #ccc;
filter: hue-rotate(30deg);
-webkit-filter: hue-rotate(30deg);
-moz-filter: hue-rotate(30deg);
-o-filter: hue-rotate(30deg);
/*
filter: saturate(0);
-webkit-filter: saturate(0);
-moz-filter: saturate(0);
-o-filter: saturate(0);
*/
}
.jumbotron hr {
width: 50%;
}
.jumbotron h4 {
max-width: 50%;
margin: 0 auto 20px;
}
/*-- Custom Buttons --*/
.btn:focus,.btn:active { /* Prevent clue outline around boostrap buttons */
outline: none !important;
}
.btn-clear-light, .btn-clear-light:visited {
color: #fff;
background: none;
border: 1px solid #fff;
border-radius: 0;
}
.btn-clear-light:hover {
color: #333;
background: #eee;
}
.btn-clear-light:active, .btn-clear-light:focus {
color: #333;
background-color: #ddd;
}
/* Flaired edges, by Tomas Theunissen */
hr.style-seven {
height: 30px;
border-style: solid;
border-color: #eee;
border-width: 1px 0 0 0;
border-radius: 20px;
}
hr.style-seven:before { /* Not really supposed to work, but does */
display: block;
content: "";
height: 30px;
margin-top: -31px;
border-style: solid;
border-color: #eee;
border-width: 0 0 1px 0;
border-radius: 20px;
}
/*-----------------
* Mobile Settings *
-----------------*/
/* Make sure parallax image fills jumbotron */
@media(max-width: 1200px) {
.jumbotron .parallax-img {
width: auto;
height: 100%;
}
}
/*-- iPhone 6+ --*/
@media(max-width: 736px) {
/* Expand feature image size */
.jumbotron .profile-img {
width: 66%;
}

html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<div class="jumbotron">
<div class="container">
<img src="http://jimkulakowski.com/web-design/img/parallax-backgrounds/space.jpg" alt="Image of mac laptop on a desk" class="parallax-img">
<img src="http://jimkulakowski.com/web-design/img/jim-at-piano.png" alt="jim at piano" class="profile-img">
<h1>Jim Kulakowski</h1>
<hr class="style-seven" />
<h4>Web Designer - Graphic Artist - User Interface Designer - Interactive Media Designer</h4>
<p>
<a class="btn btn-lg btn-clear-light soft-transition" href="https://twitter.com/j_kula" role="button" target="_blank"><i class="fa fa-twitter"> Twitter</i></a>
<a class="btn btn-lg btn-clear-light soft-transition" href="https://github.com/jmkulakowski" role="button" target="_blank"><i class="fa fa-github-square"> Github</i></a>
<a class="btn btn-lg btn-clear-light soft-transition" href="https://www.linkedin.com/in/jkulakowski
" role="button" target="_blank"><i class="fa fa-linkedin-square"> LinkedIn</i></a>
</p>
</div>
</div>
<div class="container-fluid">
<!-- Fixed Navbar -->
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<div class="container">
<!-- Mobile menu button -->
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- Site Branding -->
<div class="navbar-header">
<a class="navbar-brand" href="#">
<img src="http://jimkulakowski.com/web-design/img/jk-logo-space.png" alt="Jim Kulakowski Space Logo" class="site-logo">
<p>Jim Kulakowski</p>
</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-right" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="#portfolio">Portfolio</a></li>
<li><a href="#about">About</a></li>
<li><a href="#skills">Skills</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div>
<!-- END .navbar-collapse -->
</div>
<!-- END .container -->
</div>
<!-- END .container-fluid -->
</nav>
</div>

example2

https://codepen.io/j_holtslander/pen/waQQYm

1
<link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css" rel="stylesheet">

html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<nav class="navbar navbar-default" role="navigation" >
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse"
data-target="#example-navbar-collapse">
<span class="sr-only">切换导航</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">菜鸟教程</a>
</div>
<div class="collapse navbar-collapse" id="example-navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="#">iOS</a></li>
<li><a href="#">SVN</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Java <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="#">jmeter</a></li>
<li><a href="#">EJB</a></li>
<li><a href="#">Jasper Report</a></li>
<li class="divider"></li>
<li><a href="#">分离的链接</a></li>
<li class="divider"></li>
<li><a href="#">另一个分离的链接</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<div id="myCarousel" class="carousel slide">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- Wrapper for Slides -->
<div class="carousel-inner">
<div class="item active">
<!-- Set the first background image using inline CSS below. -->
<div class="fill" style="background-image:url('http://www.marchettidesign.net/demo/optimized-bootstrap/code.jpg');"></div>
<div class="carousel-caption">
<h2 class="animated fadeInLeft">Caption Animation</h2>
<p class="animated fadeInUp">Lorem ipsum dolor sit amet consectetur adipisicing elit</p>
<p class="animated fadeInUp"><a href="#" class="btn btn-transparent btn-rounded btn-large">Learn More</a></p>
</div>
</div>
<div class="item">
<!-- Set the second background image using inline CSS below. -->
<div class="fill" style="background-image:url('http://www.marchettidesign.net/demo/optimized-bootstrap/conference.jpg');"></div>
<div class="carousel-caption">
<h2 class="animated fadeInDown">Caption Animation</h2>
<p class="animated fadeInUp">Lorem ipsum dolor sit amet consectetur adipisicing elit</p>
<p class="animated fadeInUp"><a href="#" class="btn btn-transparent btn-rounded btn-large">Learn More</a></p>
</div>
</div>
<div class="item">
<!-- Set the third background image using inline CSS below. -->
<div class="fill" style="background-image:url('http://www.marchettidesign.net/demo/optimized-bootstrap/campus.jpg');"></div>
<div class="carousel-caption">
<h2 class="animated fadeInRight">Caption Animation</h2>
<p class="animated fadeInRight">Lorem ipsum dolor sit amet consectetur adipisicing elit</p>
<p class="animated fadeInRight"><a href="#" class="btn btn-transparent btn-rounded btn-large">Learn More</a></p>
</div>
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="icon-prev"></span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="icon-next"></span>
</a>
</div>

css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
.carousel,
.item {
height: 620px;
}
/* Make sure parallax image fills jumbotron */
@media(max-width: 1200px) {
.carousel,
.item {
height: 620px;
}
}
/*-- iPhone 6+ --*/
@media(max-width: 736px) {
/* Expand feature image size */
.carousel,
.item {
height: 580px;
}
}
.carousel-inner {
height: 100%;
background: #000;
}
.carousel-caption{padding-bottom:80px;}
.carousel-caption h2{font-size: 50px;}
.carousel-caption p{padding: 10px;}
/* Background images are set within the HTML using inline CSS, not here */
.fill {
width: 100%;
height: 100%;
background-position: center;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
opacity:0.6;
}
/**
* Button
*/
.btn-transparent {
background: transparent;
color: #fff;
border: 2px solid #fff;
}
.btn-transparent:hover {
background-color: #fff;
}
.btn-rounded {
border-radius: 70px;
}
.btn-large {
padding: 11px 45px;
font-size: 18px;
}
/**
* Change animation duration
*/
.animated {
-webkit-animation-duration: 1.5s;
animation-duration: 1.5s;
}
@-webkit-keyframes fadeInRight {
from {
opacity: 0;
-webkit-transform: translate3d(100px, 0, 0);
transform: translate3d(100px, 0, 0);
}
to {
opacity: 1;
-webkit-transform: none;
transform: none;
}
}
@keyframes fadeInRight {
from {
opacity: 0;
-webkit-transform: translate3d(100px, 0, 0);
transform: translate3d(100px, 0, 0);
}
to {
opacity: 1;
-webkit-transform: none;
transform: none;
}
}
.fadeInRight {
-webkit-animation-name: fadeInRight;
animation-name: fadeInRight;
}
.navbar.navbar-default{
margin-bottom: 0px;
background-color:#fff;
}

js

1
2
3
4
5
6
7
8
9
10
11
var mn = $(".navbar.navbar-default");
var mns = "navbar-fixed-top";
var hdr = $('.carousel').height();
$(window).scroll(function() {
if( $(this).scrollTop() > (hdr+80) ) {
mn.addClass(mns);
} else {
mn.removeClass(mns);
}
});

example 3: toggle navbar when you scroll up or the down pages

js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
jQuery(document).ready(function ($) {
var mn = $(".iu");
var mns = $(".yu");
var mnn= $(".pu");
var hdr = $('.yt').height();
var hdu = $('.yi').height();
var hde = $(document).scrollTop();
$(window).scroll(function() {
var hdd = $(document).scrollTop();
if( hdd < hde ) {
mn.fadeIn(500);
mns.fadeOut(500);
mnn.fadeOut(500);
}
else if(hdd>hde){
if (hdd>(hdr+hdu)) {
mn.fadeOut(500);
mnn.fadeIn(500);
mns.fadeOut(500);
}else if(hdd>hdr){
mn.fadeOut(500);
mns.fadeIn(500);
mnn.fadeOut(500);
} else{
mn.fadeOut(500);
mns.fadeIn(500);
mnn.fadeOut(500);
}
}
hde=hdd;
});
})

html

3 navbar like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<div class="container-fluid">
<nav class="iu navbar navbar-default navbar-fixed-top" role="navigation" >
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse"
data-target="#example-navbar-collapse">
<span class="sr-only">切换导航</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">菜鸟教程</a>
</div>
<div class="collapse navbar-collapse" id="example-navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">iOS</a></li>
<li><a href="#">SVN</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Java <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="#">jmeter</a></li>
<li><a href="#">EJB</a></li>
<li><a href="#">Jasper Report</a></li>
<li class="divider"></li>
<li><a href="#">分离的链接</a></li>
<li class="divider"></li>
<li><a href="#">另一个分离的链接</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<nav class="yu"></nav>
<nav class="pu"></nav>
</div>
<div class="yt container-fluid">
</div>
<div class="yi container-fluid">
</div>
<div class="yo container-fluid">
</div>

css

1
2
3
4
5
6
7
8
.yu{display:none;}
.yt{height: 300px;
background-color: #999;}
.yi{height: 500px;
background-color: #111;}
.yo{height: 1500px;
background-color: #222;}
.pu{display:none;}