This is something I have done more than once before. With some frustration, though. The same effect can be achieved with CSS's position: fixed, but you still need JS to slap on the class or property at the right time.
Keep in mind that that IE does not supprt these two at all. Edge does, but to be safe, use
pageXOffset - these are aliases for
scrollY. See the docs for scrollY for an explanation.