Jump to content

How can I make my navigation bar lock on to the top of the page only after you scroll down to it, sort of like the navigation bar at http://www.freedom.tm/ .

 

I need it for the second section of my site. I need it lock on the top because it's a long page. Any help would be so awesome!

Link to comment
https://linustechtips.com/topic/219966-how-to-replicate-this/
Share on other sites

Link to post
Share on other sites

A simple way to do this is by adding a scroll event listener and changing the position of the element once the client scrolls past it.

 

Essentially:

 

- Get position of element relative to the page

- Compare current scroll position to element's position

- Set position property to fixed and top/left properties to 0 if the scroll position is >= the element position

- Reset the position to its default if scroll position <= the element position

 

Items 3 and 4 could be done without an if statement using CSS.

 

You could also use a library such as Waypoints (jQuery plugin), but it's likely unnecessary for your uses.

Link to comment
https://linustechtips.com/topic/219966-how-to-replicate-this/#findComment-3015137
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×