jQuery scrollTop() Method
Last Updated :
10 Jul, 2023
The scrollTop() method is an inbuilt method in jQuery which is used to return the top vertical position of the scrollbar.
Syntax:
$(selector).scrollTop(position)
Parameters: This method accepts a single parameter positiona which is optional. It is used to specify vertical scrollbar position in pixels.
Return Value: This method returns the top position of the scroll bar.
The below example illustrates the scrollTop() method in jQuery:
Example:
html
<!DOCTYPE html>
< html >
< head >
< title >scrollTop method</ title >
< script src =
</ script >
< script >
$(document).ready(function () {
$("button").click(function () {
alert($("div").scrollTop() + " px");
});
});
</ script >
< style >
div {
border: 1px solid black;
width: 100px;
height: 150px;
overflow: auto;
}
</ style >
</ head >
< body >
< center >
< div >
Welcome to GeeksforGeeks!. Welcome to GeeksforGeeks!. Welcome
to GeeksforGeeks!. Welcome to GeeksforGeeks!. Welcome to
GeeksforGeeks!. Welcome to GeeksforGeeks!. Welcome to
GeeksforGeeks!. Welcome to GeeksforGeeks!. Welcome to
GeeksforGeeks!. Welcome to GeeksforGeeks!. Welcome to
GeeksforGeeks!. Welcome to GeeksforGeeks!. Welcome to
GeeksforGeeks!. Welcome to GeeksforGeeks!. Welcome to
GeeksforGeeks!. Welcome to GeeksforGeeks!
</ div >
< br >
< button >Click Here !</ button >
</ center >
</ body >
</ html >
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...