Sticky Navigation Bar with CSS, Fixed Navbar

HTML
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script> </head> <body> <div class="wrapper"> <div class="nav"> Sticky Navigation </div> <div class="content"> <p>Candy canes tart pie biscuit. Cupcake liquorice cake dessert tootsie roll applicake pastry. Soufflé chocolate bar macaroon powder macaroon fruitcake donut. Apple pie biscuit wypas toffee caramels wafer lollipop cotton candy. Sweet roll apple pie chupa chups tart bonbon. Croissant carrot cake gummi bears. Sweet roll wypas macaroon fruitcake gummi bears halvah tootsie roll faworki. Wypas halvah dessert carrot cake candy canes tiramisu jelly beans gingerbread. Sweet tootsie roll pie wypas macaroon jelly-o oat cake sesame snaps dessert. Icing tootsie roll gummies topping. Chocolate chocolate cake marshmallow jujubes. Liquorice lemon drops tart. Jujubes gingerbread toffee candy canes.</p> <p>Cheesecake gummies applicake cotton candy dragée wafer candy canes sugar plum sweet roll. Applicake gummi bears wafer bonbon tiramisu wypas. Chocolate cake sweet roll faworki jujubes dragée liquorice chocolate. Sugar plum halvah marzipan apple pie. Tootsie roll faworki bonbon candy canes croissant topping jelly-o tootsie roll topping. Lemon drops tiramisu chocolate. Bonbon lemon drops brownie danish. Bear claw jelly soufflé brownie dragée wafer chocolate sesame snaps. Apple pie donut jelly-o chupa chups croissant jelly-o powder gingerbread chocolate. Lollipop cupcake sweet roll chocolate cake bear claw sweet roll powder applicake oat cake. Jujubes chupa chups jujubes liquorice danish bonbon apple pie topping. Lollipop jujubes halvah carrot cake jelly-o cookie.</p> <p>Powder marshmallow bear claw topping. Chocolate bar icing brownie jujubes pie croissant chupa chups. Tootsie roll fruitcake candy. Tootsie roll jelly-o caramels candy. Gingerbread chocolate cake lemon drops. Pie toffee chupa chups brownie marshmallow cupcake croissant bear claw pudding. Ice cream oat cake marshmallow. Chocolate chocolate oat cake caramels toffee. Sesame snaps brownie cookie candy donut wypas wypas icing. Wypas sweet candy canes apple pie. Croissant gingerbread faworki. Gingerbread pie caramels halvah tiramisu carrot cake lollipop sesame snaps.</p> <p>Jujubes icing carrot cake powder jelly-o sesame snaps toffee pie jelly-o. Carrot cake sugar plum chupa chups bonbon toffee sweet roll topping jujubes cotton candy. Pie sweet roll tart chocolate bar toffee. Apple pie pastry gummies gummi bears jelly-o jujubes. Danish bear claw biscuit croissant. Muffin jujubes jujubes. Sesame snaps oat cake jelly-o jelly gummi bears bear claw. Bear claw halvah sweet roll jujubes faworki lollipop tart wafer tart. Applicake ice cream wafer gummi bears bear claw liquorice caramels marzipan bonbon. Donut cotton candy candy chocolate dessert cookie faworki. Jelly powder applicake jelly beans. Biscuit halvah dessert pie gingerbread powder fruitcake. Pudding fruitcake brownie applicake danish. Danish danish sweet roll.</p> <p>Wafer jelly tiramisu cotton candy faworki cookie bear claw bonbon. Marzipan cheesecake sugar plum pudding croissant cookie wafer. Bonbon dessert toffee gingerbread jelly beans tart tiramisu marzipan. Soufflé lemon drops wypas sweet roll apple pie ice cream. Topping lollipop cake soufflé oat cake toffee candy canes biscuit sweet. Tootsie roll bear claw dragée fruitcake chupa chups marzipan sweet roll. Wafer candy candy dragée jelly beans muffin marzipan. Oat cake bear claw lollipop jujubes cake macaroon biscuit candy canes. Chocolate cake halvah tart muffin gummi bears. Chocolate cake faworki bear claw oat cake macaroon. Candy croissant marzipan macaroon apple pie. Cake ice cream jelly.</p> <p>Cheesecake gummies applicake cotton candy dragée wafer candy canes sugar plum sweet roll. Applicake gummi bears wafer bonbon tiramisu wypas. Chocolate cake sweet roll faworki jujubes dragée liquorice chocolate. Sugar plum halvah marzipan apple pie. Tootsie roll faworki bonbon candy canes croissant topping jelly-o tootsie roll topping. Lemon drops tiramisu chocolate. Bonbon lemon drops brownie danish. Bear claw jelly soufflé brownie dragée wafer chocolate sesame snaps. Apple pie donut jelly-o chupa chups croissant jelly-o powder gingerbread chocolate. Lollipop cupcake sweet roll chocolate cake bear claw sweet roll powder applicake oat cake. Jujubes chupa chups jujubes liquorice danish bonbon apple pie topping. Lollipop jujubes halvah carrot cake jelly-o cookie.</p> <p>Powder marshmallow bear claw topping. Chocolate bar icing brownie jujubes pie croissant chupa chups. Tootsie roll fruitcake candy. Tootsie roll jelly-o caramels candy. Gingerbread chocolate cake lemon drops. Pie toffee chupa chups brownie marshmallow cupcake croissant bear claw pudding. Ice cream oat cake marshmallow. Chocolate chocolate oat cake caramels toffee. Sesame snaps brownie cookie candy donut wypas wypas icing. Wypas sweet candy canes apple pie. Croissant gingerbread faworki. Gingerbread pie caramels halvah tiramisu carrot cake lollipop sesame snaps.</p> </div> </div> </body> </html>
CSS
body { margin: 0; padding: 0; background-color: #F1FAEE; } .nav { padding: 25px 0; background-color: #457B9D; position: -webkit-sticky; } .header, .nav { text-align: center; color: #fff; } .content { width: 600px; margin: 10px auto 100px; } .sticky { position: fixed; width: 100%; left: 0; top: 0; z-index: 100; border-top: 0; }
JAVASCRIPT
$(document).ready(function() { // grab the initial top offset of the navigation var stickyNavTop = $('.nav').offset().top; // our function that decides weather the navigation bar should have "fixed" css position or not. var stickyNav = function(){ var scrollTop = $(window).scrollTop(); // our current vertical position from the top // if we've scrolled more than the navigation, change its position to fixed to stick to top, // otherwise change it back to relative if (scrollTop > stickyNavTop) { $('.nav').addClass('sticky'); } else { $('.nav').removeClass('sticky'); } }; stickyNav(); // and run it again every time you scroll $(window).scroll(function() { stickyNav(); }); });
Expand for more options Login