For example http://twitter.github.com/bootstrap/base-css.html try to scroll up. You will see the bar with
“Typography Code Tables Forms Buttons Icons by Glyphicons”
when you scroll pass the element it will add a class to make the element change. While you are scrolling back up it will remove the attribute to change the element again.
Edit: I found out this is call Sticky Element.
THat jQUery plugin can do what you want: http://imakewebthings.com/jquery-waypoints/
And here is an exemple taken from the URL:
Cheers