How we can create a unique type of navigation bar using HTML, CSS, and JS? Solution: See this Smart Fixed Navigation Items With CSS and jQuery, Fixed Navbar.
Previously I have shared sticky navbar program, but it is a unique and smart navigation bar. Basically, navigation is a list of important links to the website, which helps users to navigate those links easily. Navbar Contains links like Home, About, Contact, Services, etc. Suppose you have surfed many links across the website and now you want to go back home, but if the home link is not available in the navigation bar then you have to go back many times or put links manually.
Today you will learn to create a unique fixed navbar using jQuery & CSS. Basically, there is a navbar on top with some links when you will scroll down then a hamburger menu will reveal on the bottom side. After clicking on that hamburger menu, you will see the same navigation items. In small screens like a mobile you can’t see the top navbar you will see only the bottom fixed hamburger menu.
So, Today I am sharing Smart Fixed Navigation Items With CSS and jQuery. There I have used CSS for styling and jQuery for functioning, also jQuery modifies CSS values according to user’s action. As we know jQuery is a JS library, that’s why I am putting this in the JavaScript category. This a complete and smart fixed navigation, you can use this on your website.
If you are thinking now how this fixed nav actually is, then see the preview given below.
Preview Of Fixed Navbar With Hamburger Style
See this video preview to getting an idea of how this menu program looks like.
Now you can see this visually, also you can see it live by pressing the button given above. If you like this, then get the source code of its.
You May Also Like:
Smart Fixed Navigation Items With CSS and jQuery Source Code
Before sharing source code, let’s talk about it. First I have created a header section for placing the logo and created the main div for placing menu items and dummy contents. Inside the main div, I have placed a nav tag and put a list inside it and created a tag and placed dummy images inside it. Items like navigation links and image layout are based on HTML list <ul> & <li> tags.
Now using CSS I have placed all the elements in the right place, as you can see in the preview. All images are powered by placeholder.com, which is a dummy image provider. There I have used CSS @media query for creating a responsive design. And used @keyframe command for creating animation effects.
jQuery handling here the main features of the program. There I have used jQuery .scrollTop command for getting scroll position and reveal/hide hamburger menu. After that, I have used if{} else{} statements and adding/removing CSS values according to action. Left all other things you will understand after getting the codes, I can’t explain all in writing.
For creating this program you have to create 3 files for that. First file for HTML, second for CSS, and the third file for JavaScript. Follow the steps to creating this without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
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 |
<!DOCTYPE html> <!-- Code By Webdevtrick ( https://webdevtrick.com ) --> <html lang="en" > <head> <meta charset="UTF-8"> <title>Smart Fixed Navigation | Webdevtrick.com</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js"></script> <link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,700' rel='stylesheet'> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <link rel="stylesheet" href="style.css"> </head> <body> <header> <div id="logo"><a href="#0"><img src="https://via.placeholder.com/120x50?text=LOGO" alt="Logo"></a></div> <h1>Smart Fixed Navigation</h1> </header> <div id="nav"> <a href="#0" class="nav-box">Menu<span></span></a> <nav id="nav-items"> <ul> <li><a href="#0">Home</a></li> <li><a href="#0">Services</a></li> <li><a href="#0">Gallery</a></li> <li><a href="#0">Blog</a></li> <li><a href="#0">Contact</a></li> </ul> </nav> </div> <main> <ul id="imagesBox" class="image-container"> <li> <img src="https://via.placeholder.com/550x550?text=Image" alt=""> </li> <li> <img src="https://via.placeholder.com/550x550?text=Image" alt=""> </li> <li> <img src="https://via.placeholder.com/550x550?text=Image" alt=""> </li> <li> <img src="https://via.placeholder.com/550x550?text=Image" alt=""> </li> <li> <img src="https://via.placeholder.com/550x550?text=Image" alt=""> </li> <li> <img src="https://via.placeholder.com/550x550?text=Image" alt=""> </li> <li> <img src="https://via.placeholder.com/550x550?text=Image" alt=""> </li> <li> <img src="https://via.placeholder.com/550x550?text=Image" alt=""> </li> <li> <img src="https://via.placeholder.com/550x550?text=Image" alt=""> </li> <li> <img src="https://via.placeholder.com/550x550?text=Image" alt=""> </li> <li> <img src="https://via.placeholder.com/550x550?text=Image" alt=""> </li> <li> <img src="https://via.placeholder.com/550x550?text=Image" alt=""> </li> </ul> </main> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes given here.
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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
/* Code By Webdevtrick ( https://webdevtrick.com ) */ html * { -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } *, *:after, *:before { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } body { font-size: 100%; font-family: "Source Sans Pro", sans-serif; color: #2c3e51; background-color: #2c3e51; } a { color: #ff3a3a; text-decoration: none; } img { max-width: 100%; } .image-container { width: 90%; max-width: 1170px; margin: 0 auto; } .image-container::after { content: ''; display: table; clear: both; } header { position: relative; height: 200px; background: #ffffff; text-align: center; margin-bottom: 1em; padding-top: 3em; } header #logo { margin-bottom: 3em; } header h1 { font-size: 20px; font-size: 1.25rem; } @media only screen and (min-width: 768px) { header { margin-bottom: 4em; } } @media only screen and (min-width: 1170px) { header { height: 400px; padding-top: 11em; } header #logo { position: absolute; top: 40px; left: 5%; } header h1 { font-size: 40px; font-size: 2.5rem; font-weight: 300; } } #nav ul { position: fixed; width: 90%; max-width: 400px; right: 5%; bottom: 20px; border-radius: 0.25em; box-shadow: 0 0 10px rgba(232, 74, 100, 0.4); background: #ffffff; visibility: hidden; overflow: hidden; z-index: 1; -webkit-backface-visibility: hidden; backface-visibility: hidden; transform: scale(0); transform-origin: 100% 100%; transition: transform 0.3s, visibility 0s 0.3s; } #nav ul li { -webkit-backface-visibility: hidden; backface-visibility: hidden; } #nav ul.is-visible { visibility: visible; transform: scale(1); transition: transform 0.3s, visibility 0s 0s; } #nav ul.is-visible li:nth-child(1) { animation: cd-slide-in 0.2s; } #nav ul.is-visible li:nth-child(2) { animation: cd-slide-in 0.3s; } #nav ul.is-visible li:nth-child(3) { animation: cd-slide-in 0.4s; } #nav ul.is-visible li:nth-child(4) { animation: cd-slide-in 0.5s; } #nav ul.is-visible li:nth-child(5) { animation: cd-slide-in 0.6s; } #nav li a { display: block; padding: 1.6em; border-bottom: 1px solid #eff2f6; } #nav li:last-child a { border-bottom: none; } @media only screen and (min-width: 1170px) { #nav ul { position: absolute; width: auto; max-width: none; bottom: auto; top: 36px; background: transparent; visibility: visible; box-shadow: none; transform: scale(1); transition: all 0s; } #nav li { display: inline-block; } #nav li a { opacity: 1; padding: .4em; margin-left: 1.6em; border-bottom: none; } #nav li a:hover { color: #00bd9b; } #nav.is-fixed ul { position: fixed; width: 90%; max-width: 400px; bottom: 20px; top: auto; background: #ffffff; visibility: hidden; box-shadow: 0 0 10px rgba(232, 74, 100, 0.4); transform: scale(0); } #nav.is-fixed ul li { display: block; } #nav.is-fixed ul li a { padding: 1.6em; margin-left: 0; border-bottom: 1px solid #eff2f6; } #nav ul.has-transitions { transition: transform 0.3s, visibility 0s 0.3s; } #nav ul.is-visible { visibility: visible; transform: scale(1); transition: transform 0.3s, visibility 0s 0s; } #nav ul.is-hidden { transform: scale(0); transition: transform 0.3s; } } .nav-box { position: fixed; bottom: 20px; right: 5%; width: 44px; height: 44px; background: #ffffff; border-radius: 0.25em; box-shadow: 0 0 10px rgba(232, 74, 100, 0.4); overflow: hidden; text-indent: 100%; white-space: nowrap; z-index: 2; } .nav-box span { position: absolute; display: block; width: 20px; height: 2px; background: #e84a64; top: 50%; margin-top: -1px; left: 50%; margin-left: -10px; transition: background 0.3s; } .nav-box span::before, .nav-box span::after { content: ''; position: absolute; left: 0; background: inherit; width: 100%; height: 100%; transform: translateZ(0); -webkit-backface-visibility: hidden; backface-visibility: hidden; transition: transform 0.3s, background 0s; } .nav-box span::before { top: -6px; transform: rotate(0); } .nav-box span::after { bottom: -6px; transform: rotate(0); } .nav-box.menu-is-open { box-shadow: none; } .nav-box.menu-is-open span { background: rgba(232, 74, 100, 0); } .nav-box.menu-is-open span::before, .nav-box.menu-is-open span::after { background: #e84a64; } .nav-box.menu-is-open span::before { top: 0; transform: rotate(135deg); } .nav-box.menu-is-open span::after { bottom: 0; transform: rotate(225deg); } @media only screen and (min-width: 1170px) { .nav-box { visibility: hidden; transform: scale(0); transition: transform 0.3s, visibility 0s 0.3s; } .is-fixed .nav-box { visibility: visible; transition: all 0s; transform: scale(1); animation: cd-bounce-in 0.3s linear; } } #imagesBox li { margin-bottom: 1.5em; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2); border-radius: 0.25em; } #imagesBox li img { width: 100%; display: block; border-radius: 0.25em; } @media only screen and (min-width: 768px) { #imagesBox li { width: 48%; float: left; margin-bottom: 2em; margin-right: 4%; } #imagesBox li:nth-child(2n) { margin-right: 0; } } @media only screen and (min-width: 1170px) { #imagesBox li { width: 31%; float: left; margin-bottom: 2.5em; margin-right: 3.5%; } #imagesBox li:nth-child(2n) { margin-right: 3.5%; } #imagesBox li:nth-child(3n) { margin-right: 0; } } @keyframes cd-slide-in { 0% { transform: translateX(100px); } 100% { transform: translateY(0); } } @keyframes cd-bounce-in { 0% { transform: scale(0); } 60% { transform: scale(1.2); } 100% { transform: scale(1); } } |
function.js
The final step, create a JavaScript file named ‘function.js‘ and put the codes.
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 |
// Code By Webdevtrick ( https://webdevtrick.com ) jQuery(document).ready(function($){ // browser window scroll (in pixels) after which the "menu" link is shown var offset = 300; var navigationContainer = $('#nav'), mainNavigation = navigationContainer.find('#nav-items ul'); //hide or show the "menu" link checkMenu(); $(window).scroll(function(){ checkMenu(); }); //open or close the menu clicking on the bottom "menu" link $('.nav-box').on('click', function(){ $(this).toggleClass('menu-is-open'); //we need to remove the transitionEnd event handler (we add it when scolling up with the menu open) mainNavigation.off('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend').toggleClass('is-visible'); }); function checkMenu() { if( $(window).scrollTop() > offset && !navigationContainer.hasClass('is-fixed')) { navigationContainer.addClass('is-fixed').find('.nav-box').one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(){ mainNavigation.addClass('has-transitions'); }); } else if ($(window).scrollTop() <= offset) { //check if the menu is open when scrolling up if( mainNavigation.hasClass('is-visible') && !$('html').hasClass('no-csstransitions') ) { //close the menu with animation mainNavigation.addClass('is-hidden').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){ //wait for the menu to be closed and do the rest mainNavigation.removeClass('is-visible is-hidden has-transitions'); navigationContainer.removeClass('is-fixed'); $('.nav-box').removeClass('menu-is-open'); }); //check if the menu is open when scrolling up - fallback if transitions are not supported } else if( mainNavigation.hasClass('is-visible') && $('html').hasClass('no-csstransitions') ) { mainNavigation.removeClass('is-visible has-transitions'); navigationContainer.removeClass('is-fixed'); $('.nav-box').removeClass('menu-is-open'); //scrolling up with menu closed } else { navigationContainer.removeClass('is-fixed'); mainNavigation.removeClass('has-transitions'); } } } }); |
That’s It. Now you have successfully created Smart Fixed Navigation Items With CSS and jQuery, Fixed Navbar Program. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.