jQuery scrollTop()

Introduction

Return the vertical scrollbar position for a <div> element:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/*from   w  w w .  jav  a 2 s  .c om*/
<script>
$(document).ready(function(){
  $("button").click(function(){
    document.getElementById("demo").innerHTML = $("div").scrollTop() + " px";
  });
});
</script>
</head>
<body>

<p id="demo"></p>
<div style="border:1px solid black;width:100px;height:150px;overflow:auto">
This is some text. This is some text. This is some text. This is some text. 
This is some text. This is some text. This is some text. This is some text. 
This is some text.</div><br>

<button>test</button>
<p>Move the scrollbar down and click the button again.</p>

</body>
</html>

The scrollTop() method sets or gets the vertical scrollbar position for the selected elements.

As getter, this method returns the vertical position of the scrollbar for the first matched element.

As setter, this method sets the vertical position of the scrollbar for all matched elements.

Return vertical scrollbar position:

$(selector).scrollTop()

Set vertical scrollbar position:

$(selector).scrollTop(position)
Parameter Description
position Specifies the vertical scrollbar position in pixels



PreviousNext

Related