Calculate the distance to the bottom of a page

Pixels left to scroll before the end of the page.

JavaScriptBrowser · DOM Version 1.0.0 · MIT · .md

calculate-the-distance-to-the-bottom-of-a-page.js
function distanceToBottom() { return document.documentElement.scrollHeight - window.innerHeight - window.scrollY; }

Usage

Markup

fixture.html
<div style="height: 3000px">A 3000 px tall page</div>

Script

usage.js
// A 3000 px tall page in a 720 px tall viewport:
distanceToBottom(); // → 2280
window.scrollTo(0, 280);
distanceToBottom(); // → 2000

Lines marked → were executed in headless Chromium against the markup above, and each result was compared with the value shown. Lines marked e.g. are illustrative.

Know the limits

  • document.documentElement.scrollHeight measures the whole page. The original used document.body.scrollHeight, which can differ when body has margins or a fixed height.
  • Scroll positions can be fractional on zoomed or high-density screens: compare with a 1 px tolerance.
  • For infinite scrolling, an IntersectionObserver on a sentinel element avoids measuring on every scroll event.

The function above is the code the usage lines run. It does not validate its arguments; add checks where it receives untrusted input.

Source & maintenance

Based on Calculate the distance to the bottom of a page from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.

A function measuring document.documentElement instead of a value from document.body. Executed in Chromium. Original MDX retained as inert source evidence.

Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.

Usage lines executed in headless Chromium against the fixture markup; no external services · reviewed October 28, 2026.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.