Find Out How Far Down User Has Scrolled With JavaScript

May 19, 2020 by Andreas Wik

You can easily find out how far down on the page the user has scrolled by checking the window or document object.

So, depending on the browser, we can use either the pageYOffset property from the window object, or the scrollTop property from document.

Below I assign the value to a variable named scrollTop:

const scrollTop = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop

 

CodePen demo:

See the Pen How far has the user scrolled down by Andreas Wik (@andreaswik) on CodePen.

 

Share this article

Recommended articles

@awikio