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

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Page Details / Get and Set Scroll Position of an Element

Get and Set Scroll Position of an Element

Get and Set Scroll Position of an Element

To set or get the scroll position of an element, you use the properties scrollTop and scrollLeft of the element.

The scrollLeft and scrollTop properties return the number of pixels that the element’s content is scrolled from its left and top edges. The top left corner of the element is (0, 0). The scrollLeft and scrollTop are relative to that coordinate.

The following shows how to get the scroll position of the element with the class .btn:

const el = document.querySelector('.btn');
// get scroll position in px
console.log(el.scrollLeft, el.scrollTop);

And the following shows how to set the scroll position of the .btn element:

const el = document.querySelector('.btn');
// set scroll position in px
el.scrollLeft = 300;
el.scrollTop = 500;

Source

https://www.javascripttutorial.net/dom/css/get-and-set-scroll-position-of-an-element/

Page Details

Related Snippets:

  • Viewport Width
  • Get the size of an image
  • Get the Width and Height of the Window
  • Check if the browser tab of the page is focused

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers