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

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Forum
  • 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:

  • Delayed Image Loader
  • Find the distance from a given element to the top of the document
  • Copyright Notice
  • Get the size of an image

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2022 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us