The cleanest example I have seen for this feature is by Sergey Akopov.
Sergey implement a jQuery example that works wonderfully! Here is the link to the article. I attached the ZIP file same code, which is on Sergey's site as well.
MaintainScroll.zip (3.01 kb)
Here is a link to a pure JavaScript example (older 2006). I have not tried it out but wanted to save the link as another example.