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....
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.
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.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With