.scrollTop()

Get the current vertical position of the scroll bar for the first element in the set of matched elements or set the vertical position of the scroll bar for every matched element. Set the current vertical position of the scroll bar for each of the set of matched elements.

.scrollTop()🡢 Number

The vertical scroll position is the same as the number of pixels that are hidden from view above the scrollable area. If the scroll bar is at the very top, or if the element is not scrollable, this number will be 0.

Get the scrollTop of a paragraph.

JS
<p>Hello</p>
<p></p>
CSS
p {
  margin: 10px;
  padding: 5px;
  border: 2px solid #666;
}
HTML
var p = $("p").first();
$("p")
  .last()
  .text("scrollTop:" + p.scrollTop());
DEMO

.scrollTop(value)🡢 jQuery

value NumberA number indicating the new position to set the scroll bar to.

The vertical scroll position is the same as the number of pixels that are hidden from view above the scrollable area. Setting the scrollTop positions the vertical scroll of each matched element.

Set the scrollTop of a div.

JS
<div class="demo">
  <h1>lalala</h1>
  <p>Hello</p>
</div>
CSS
div.demo {
  background: #ccc none repeat scroll 0 0;
  border: 3px solid #666;
  margin: 5px;
  padding: 5px;
  position: relative;
  width: 200px;
  height: 100px;
  overflow: auto;
}
p {
  margin: 10px;
  padding: 5px;
  border: 2px solid #666;
  width: 1000px;
  height: 1000px;
}
HTML
$("div.demo").scrollTop(300);
DEMO

Looking for a Web Developer?

👋

Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)