Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an element fake position:fixed so it acts fixed until a certain scroll height, then attaches?

A number of websites have a feature, where an element appears fixed on the page as you scroll UNTIL you hit a certain point, like the end of a side bar, and then it anchors to the bottom of that sidebar. Once you scroll back up, it begins to act like a fixed element, remaining on your screen as you scroll.

What do you call this and how is it done?

like image 872
Wesley Avatar asked Feb 29 '12 19:02

Wesley


1 Answers

You can set position to absolute and attached scroll event to the page and in that event you change top css value based on position of scrollbar (in jQuery it's scrollTop in pure javascript it should be similar), and then you add condition that top is changed only if scrollTop is less then specific value like offset.top + height of the sidebar.

like image 85
jcubic Avatar answered Oct 18 '22 15:10

jcubic