• 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
  • VBA
  • 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.  Using JavaScript to Replace a Character at an Index
  • 2.  Using JavaScript to Remove Forward Slash From String
  • 3.  How to Repeat Character N Times in JavaScript
  • 4.  JavaScript indexOf – Get the position of a value within a string
  • 5.  Using JavaScript to Set the Id of an Element
  • 6.  JavaScript sin – Find Sine of Number in Radians Using Math.sin()
  • 7.  lastIndexOf JavaScript – Get the Last Position of a String in Another String
  • 8.  Using JavaScript to Multiply All Elements in an Array
  • 9.  How to Empty a String in JavaScript
  • 10.  JavaScript Round to Nearest 10

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy