I did this in jQuery at one time. Hopefully this is helpful.
$(window).scroll(function() {
var offsetTop = $(window).scrollTop();
$("#target").css({top: offsetTop});
});
To explain, whenever the viewport is scrolled, the offset of the viewport to the top of the page is set (offsetTop). Then the #target element is moved to follow the viewport. That will give a fixed effect, so what you need to do is set the max offsetTop to (max depth - #target height).
Note: Dimension type calculations vary widely among browsers, so using a library like jQuery dimensions is quite helpful.
Note: Dimension type calculations vary widely among browsers, so using a library like jQuery dimensions is quite helpful.