Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep Side Navigation Fixed with Scrolling of page

Tags:

jquery

css

scroll

I have a clients website - www.stagecraft.co.uk and they want the navigation on the hire pages (longer page) to still be there at when you scroll the page down. I've had a quick go (not live) with position fixed but in doing so it the leftside navigation is about 200px or so from the top of the window. Any when to get it at the top of the window when scrolling?

Thanks in advance....

like image 665
Stuart Robson Avatar asked Jul 23 '09 13:07

Stuart Robson


People also ask

How do I get my sidebar to stay in place?

The easiest way to handle this is just to use CSS fixed positioning. Our sidebar is within a #page-wrap div with relative positioning, so the sidebar will set inside there, then we just push it over into place with margin. With this technique, the sidebar stays solidly in place as you scroll down the page.

How do I make my sidebar scroll sticky?

You can either make a custom stylesheet specifying the sidebar style or you can add an inline CSS code "position: sticky" to your sidebar element/container.

How do I fix a header at the top of a page in CSS?

Answer: Use CSS fixed positioning You can easily create sticky or fixed header and footer using the CSS fixed positioning. Simply apply the CSS position property with the value fixed in combination with the top and bottom property to place the element on the top or bottom of the viewport accordingly.


1 Answers

You could make it position fixed, only when scrolling has reached a certain point:

$(window).scroll(function() {
    if ($(this).scrollTop() > 200) { //I just used 200 for testing
        $("#tester").css({ "position": "fixed", "top": 0 });
    } else {
        $("#tester").css({ "position": "absolute", "top": "200px" }); //same here
    }               
});

and the CSS for the div is as following:

#tester {
    position: absolute;
    right: 20px;
    top: 200px;
    height: 200px;
    width: 100px;
    background: red;
}
like image 199
peirix Avatar answered Sep 21 '22 03:09

peirix