Automatic Header stick to scroll with jQuery

Many must’ve seen this automatic-header-scroll-stick feature. Where a normal looking header suddenly sticks to the page when the page is scrolled.

Gmail to recently introduced such a “auto-sticking-interaction-header”. I thought I should give a simple explanation about it.

Demo

In the above HTML page, observe the following

  • A well defined stickyheader ID
  • jQuery inclusion
  • .. and some jQuery magic !

Explained …

At first we determine the offset of the #stickyheader, and grab the top variable of the object

Once done that, we bind the scroll event/method to the window object using jQuery magic. And to it we do the following

IF window.scrollTop exceeds the #stickyheader’s offsetTop, we simply set #stickyheader’s css as position: fixed; top: 0px. As soon as window.scrollTop drops below the offset, we make it stick back to its original position using position: static, The default positioning of the element.

Thats it!

Warning: Doesn’t work in IE6x, 7, etc or I don’t know – I DONT CARE

Thokarwadi Dam Bike Ride

May 15, 2011
Another weekend, another shot @ biking. This time it was Thokarwadi Dam, Kamshet, Taluka Maval, District Pune.

The long winding road after the left @ Kanhe village just 7km ahead of Kamshet took us through a long tar ribbon bordering the parched dams of Nethersule and Thokerwadi

Thokarwadi Dam Bike Ride

Thokarwadi Dam Bike Ride

Directions

  1. Take NH4 (Mumbai – Pune) towards Kamshet
  2. Take a left at Kanhe village after Kamshet


View Larger Map

ऋतुराज का Home Page