Auto Hides Fixed Top Navigation Menu

Auto Hides Fixed Top Navigation Menu

Today, we are going to create Auto Hides Fixed Top Navigation Menu with jQuery and CSS. We are going to use the basic jQuery plugins to do this kind of tutorials. Using it, we create fixed top navigation menu in the header of the page and it will autohide the menu when the user scrolls down the window but it will show it out again when it scrolling up the window again. Let’s take a look the tutorial below to know how to create.

Demo

How to Use it

Creating Markup

1. Create your top navigation menu of your page.
  1. <div id="menu_header" class="menu_header-min">
  2.         <a href="http://www.sourcecodester.com/">Programming</a>
  3.         <a href="http://www.sourcecodester.com/programming">>Mobile</a>
  4.         <a href="http://www.sourcecodester.com/mobile">Blog</a>
  5.         <a href="http://www.sourcecodester.com/blog">Articles</a>
  6.         <a href="http://www.sourcecodester.com/article">Tutorials</a>
  7.         <a href="http://www.sourcecodester.com/tutorial">Contact</a>
  8. </div>
2. Constructing the CSS to make it stick your navigation menu to the top of your page.
  1. #menu_header {
  2.                
  3.         background: -webkit-linear-gradient(#585858,#3d3d3d);
  4.         border:1px solid #222;
  5.         color: #fff;
  6.         position: fixed;
  7.         font-size:20px;
  8.         z-index: 1000;
  9.         display: block;
  10.         top: 0;
  11.         left: 0;
  12.         width: 100%;
  13.         padding: 30px;
  14.         text-align: center;
  15.         overflow:hidden;
  16.     -webkit-transition: 500ms all ease-in-out;
  17.         -moz-transition: 500ms all ease-in-out;
  18.         -ms-transition: 500ms all ease-in-out;
  19.         -o-transition: 500ms all ease-in-out;
  20. }
3. And, this CSS codes for the hide of your navigation menu to the top of your page.
  1. .menu_header-close {   
  2.         -moz-transform: translateY(-100%);
  3.          -ms-transform: translateY(-100%);
  4.          -webkit-transform: translateY(-100%);
  5.     transform: translateY(-100%);
  6.         opacity: 0;
  7. }
4. This is our simple body content. Kindly copy and paste to your BODY tag of your page.
  1. <div id="content">
  2.         <h1>Auto Hides Fixed Top Navigation Menu</h1>
  3.        
  4.         <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris sodales tortor mauris,
  5.         vitae lobortis ex tincidunt vulputate. Aenean ultrices libero eget rutrum suscipit. Fusce blandit
  6.         ipsum vitae nibh molestie, sit amet ultrices urna convallis. Vivamus at velit at magna ultrices pulvinar
  7.         vitae sit amet nunc. Nunc at pretium nisl. Praesent sollicitudin lacus sapien, vel sagittis leo venenatis vitae.
  8.         Praesent id velit sapien. Aliquam mattis diam eget sollicitudin rhoncus. Nam sit amet nibh ligula. Maecenas hendrerit
  9.         neque id felis gravida fermentum. Ut condimentum est sodales mi dignissim, in placerat justo pretium. Phasellus a ex nunc.</p>
  10.  
  11.         <p>Curabitur dictum ipsum ac commodo porta. Maecenas et tortor quis nisl dictum molestie in eu erat. Nunc volutpat
  12.         enim vitae enim ultrices varius. Sed vitae orci eget leo bibendum malesuada sed efficitur justo. Sed ipsum magna,
  13.         lacinia eu est pharetra, vestibulum vehicula massa. Duis in dictum enim, quis porta est. Interdum et malesuada fames
  14.         ac ante ipsum primis in faucibus. Duis malesuada, urna a ultricies lacinia, arcu neque iaculis quam, sed viverra velit
  15.         mauris vitae mi. Quisque maximus sollicitudin leo, in finibus nisl sodales mattis. Proin congue arcu at erat vestibulum,
  16.         eget tristique metus aliquam. Vestibulum feugiat venenatis diam vitae consequat. Integer non volutpat ex. Etiam egestas,
  17.         justo in hendrerit tempus, neque dolor vulputate justo, feugiat suscipit leo dolor ac nibh. Vestibulum massa eros, condimentum
  18.         sed imperdiet ac, pharetra ut tellus.</p>
  19. </div> 
5. This script you are going to use for the hide of your fixed navigation menu.
  1. <script>
  2. //scroll hide  
  3.                 var lastScrollTop = 0;         
  4.                                    
  5.             $(document).on('scroll', $.throttle(function() {
  6.                
  7.                 var stop = $(this).scrollTop();
  8.                        
  9.                         if($(window).scrollTop() > (80)) {             
  10.                 if (stop > lastScrollTop){
  11.                     console.log('down');
  12.                     // scrolling down
  13.                         if($('#menu_header').hasClass('menu_header-min'))
  14.                         {
  15.                             $('#menu_header').addClass('menu_header-close');
  16.                             $('#menu_header').removeClass('menu_header-min');
  17.                         }
  18.                     }
  19.                 else
  20.                 {
  21.                         // scrolling up
  22.                         if($('#menu_header').hasClass('menu_header-close'))
  23.                         {
  24.                             console.log('up');
  25.                             $('#menu_header').addClass('menu_header-min');
  26.                             $('#menu_header').removeClass('menu_header-close');
  27.                         }
  28.                 }
  29.                 lastScrollTop = stop;
  30.                 }
  31.                        
  32.             }, 100));
  33. </script>

Output


Result
Hope that this simple yet useful tutorials that I created may help you to your future projects. That's it, kindly click the "Download Code" button below for the full source code. And try it to customize your own Auto Hides Fixed Top Navigation Menu. Enjoy coding. If you are interested in programming, we have an example of programs that may help you even just in small ways. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment