• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Get the Scroll Position

Using JavaScript to Get the Scroll Position

April 3, 2022 Leave a Comment

While scrolling a webpage, we can use JavaScript to get the scroll position of the top of the window using the scrollTop property.

document.documentElement.scrollTop

The scrollTop property will return the top position of the element it is called on. In this case, we use it on the webpage document we are on so it will return the top position of the screen. If we have not scrolled at all, it will return 0, since that means we are at the top of the page.

Note this can also be done in jQuery using the scrollTop() method.

Using JavaScript to Get the Scroll Position While Scrolling a Webpage

In this example, we will get and show the current scroll position as we scroll this page. To make sure this page is large enough to have to scroll on, we will create a very tall div. Here is the html:


<style>#div1 { height: 4000px; width:300px; border: 1px solid #444; position: relative; } #scrolling-div { position: absolute; top: 10px; margin-left: 10px; }</style>
<div id="div1">
  <div id="scrolling-div">The scrollTop position is: <span id="scroll-number"></span></div>
</div>

We will use the onscroll event on the window object to launch a function while we scroll the page. Every time the function is called, we will use the scrollTop property on the document object to get the top position of our page.

Finally, we will use the textContent property to display the number as we scroll.

Here is the code:

window.onscroll = function() {
  document.getElementById("scroll-number").textContent = (document.documentElement.scrollTop);
  //The code below is to make sure the #scrolling-div stays in view of the user as they scroll
  if (document.documentElement.scrollTop > document.getElementById("div1").offsetTop && document.documentElement.scrollTop < (document.getElementById("div1").offsetTop + 4000)){
    document.getElementById("scrolling-div").style.position = "fixed";
  } else {
     document.getElementById("scrolling-div").style.position = "absolute";
  }
};

The final code and output for this example is below:

Code Output:

The scrollTop position is:

Full Code:


<style>#div1 { height: 4000px; width:300px; border: 1px solid #444; position: relative; } #scrolling-div { position: absolute; top: 10px; margin-left: 10px; }</style>
<div id="div1">
  <div id="scrolling-div">The scrollTop position is: <span id="scroll-number"></span></div>
</div>

<script>

window.onscroll = function() {
  document.getElementById("scroll-number").textContent = (document.documentElement.scrollTop);
  if (document.documentElement.scrollTop > document.getElementById("div1").offsetTop && document.documentElement.scrollTop < (document.getElementById("div1").offsetTop + 4000)){
    document.getElementById("scrolling-div").style.position = "fixed";
  } else {
     document.getElementById("scrolling-div").style.position = "absolute";
  }
};

</script>

Hopefully this article has helped you understand how to use JavaScript to get the scroll position.

Other Articles You'll Also Like:

  • 1.  Creating a JavaScript Function to Multiply Two Numbers
  • 2.  Using JavaScript to Round to 3 Decimal Places
  • 3.  Using Javascript to Remove the Id from a Div
  • 4.  Using JavaScript to Remove Forward Slash From String
  • 5.  outerHTML – How to Get and Change the outerHTML Property of an Element
  • 6.  Using JavaScript to Count Even Numbers in an Array
  • 7.  JavaScript Check if Attribute Exists in HTML Element
  • 8.  Insert Character Into String In JavaScript
  • 9.  How to Change the Id of an Element in JavaScript
  • 10.  JavaScript String endsWith Method

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy