Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leave menu bar fixed on top when scrolled

Tags:

jquery

css

I've seen some websites that when the user scrolls down the page a box would pop-up to the right or left...

Also, noticed this template: http://www.mvpthemes.com/maxmag/ the designer does a nice job leaving the nav bar fixed on top.

Now, how are these done? I guess it uses jquery to get the position of the page and to show the box.

Can you please guide me to where I can find a snippet so I can learn to do something like that.

like image 628
Wilson Avatar asked Nov 07 '12 17:11

Wilson


People also ask

What class could be used to keep a navigation bar at the top of the screen while scrolling?

spot class. The handler and the options give the following behavior: If the spot is completely hidden (not intersecting), we add a scrolling class to the navbar; if even 1px is visible (intersecting), we remove that class. The handleScroll method is called in both cases.


1 Answers

This effect is typically achieved by having some jquery logic as follows:

$(window).bind('scroll', function () {     if ($(window).scrollTop() > 50) {         $('.menu').addClass('fixed');     } else {         $('.menu').removeClass('fixed');     } }); 

This says once the window has scrolled past a certain number of vertical pixels, it adds a class to the menu that changes it's position value to "fixed".

For complete implementation details see: http://jsfiddle.net/adamb/F4BmP/

like image 113
adamb Avatar answered Oct 13 '22 04:10

adamb