Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sidebar that follows scroll, but scrolls self, if taller than viewport

(Hey, first post from a longtime lurker :)

I've built a simple sidebar that does the 'absolute-to-fixed' trick to stay on screen, but would like to take in account scenarios, where the sidebar is higher than the viewport.

So I came up with this idea. It all starts as in above:

  • On page load, the sidebar is drawn at starting location, some distance from viewport top.
  • When the user scrolls the page, the sidebar moves with the content
  • If sidebar fits the viewport vertically, it fixes to the top

But here it gets more dynamic:

  • If sidebar is taller than the viewport, it continues to scroll with the content until the bottom of the sidebar is reached, and it fixes there. The top of the sidebar scrolls beyond top of viewport.

  • When the user scrolls back towards page top, the sidebar moves with the content until the top of the sidebar is reached, and it fixes there. The bottom of the sidebar scrolls beyond the bottom of the viewport.

This way the sidebar reacts to scrolling as usual, while sticking around close enough to find on long pages.

Any pointers to examples, or jQuery- friendly code snippets/guidelines would be much appreciated.

like image 429
Jon Fabritius Avatar asked Dec 02 '10 21:12

Jon Fabritius


People also ask

What is a sticky sidebar?

A sticky sidebar is a web design technique to keep the sidebar on the screen even if the user has scrolled past the position where it initially displayed.

How do I make my scroll height?

To get the height of the scroll bar the offsetHeight of div is subtracted from the clientHeight of div. OffsetHeight = Height of an element + Scrollbar Height. ClientHeight = Height of an element. Height of scrollbar = offsetHeight – clientHeight.

What is scrollTop and scrollHeight?

In summary, scrollTop is how much it's currently scrolled, and scrollHeight is the total height, including content scrolled out of view.


2 Answers

I had this exact idea for a project, here's a example of how to implement it

http://jsfiddle.net/ryanmaxwell/25QaE/

like image 88
Ryan Avatar answered Sep 21 '22 19:09

Ryan


The behaviour you would like to achieve is called 'affix'. Twitter's Bootstrap front-end framework has a javascript component that can do what you would like to get. Please check the section about the affix component. In fact, there you can also see a demonstration of the desired behaviour, the menu in the left sidebar is affixed.

You can define functions for the offset parameter of the affix init call, so it can be dynamically determined when to pin/unpin the element. To see an example in context, check the source of the above linked page, specifically look for application.js, it contains the setup of the affix that you can see on the left side. Here you can see the init code taken out of context:

// side bar $('.bs-docs-sidenav').affix({   offset: {     top: function () { return $window.width() <= 980 ? 290 : 210 }   , bottom: 270   } }) 

It is probably also worth to check in the page sources the docs.css stylesheet, which contains some positioning and styling for the affixed element. Checking this might solve your problem, or at least could give you and idea.

like image 21
Temaruk Avatar answered Sep 17 '22 19:09

Temaruk