Javascript – How to detect that the client is scrolled to the top or bottom of a webpage

javascript

I'm looking for a cross-browser method of detecting that a client web browser is scrolled all the way to the bottom (or top) of the screen.

Really, the top is fairly easy, as
scrY = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop
is zero if you're at the top. The problem is that scrY seems to return the top of the scroll bar, and not the bottom, so instead of getting something equivalent to the height of the document (in pixels) I what is presumably the height of the document less the size of the scroll bar.

Is there an easy, cross-browser way to find out if the user has scrolled down to the bottom of the document/window? Most specifically, I understand general scroll bar manipulation (setting it, moving it, etc.) but how can I get the delta of the bottom of the scrollbar's position relative to the bottom of the window/document.