scrollTop property in javascript

scrollTop property in javascript

scrollTop property is used to get the number of pixels that are scrolled vertically.

If the element is not scrollable or when it doesn't contain any scroll bar, it returns 0.


var elem = element.scrollTop;

Example - Getting the scrollTop of an element

Try Yourself

Example Explanation

  • In the above example, when the button is clicked fun_name() function is called.
  • In the Javascript function, first we are getting the element (document.getElementById("idP")) and storing in attr variable.
  • Using the scrollTop property, when the content is scrolled to the top, it retrieves the total pixels has been scrolled and displaying it (alert(attr.scrollTop)).