scrollTop() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:scrollTop

Description

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

When the scrollbar is on the top end, the position is 0.

Parameter

Parameter Description
position Specifies the vertical scrollbar position in pixels

The following code shows how to Return the vertical scrollbar position for a <div> element:

Demo Code

ResultView the demo in separate window

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

<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>Return the vertical position of the scrollbar</button>
<p>Move the scrollbar down and click the button again.</p>

</body>
</html>

Related Tutorials