jQuery scrollTop Method


jQuery .scrollTop Method : is used to get or set the vertical scrollbar position of the selected element.


jQuery scrollTop Method Syntax 1 – Get The Position

Syntax for get position of scrollbar from top –

$(selector).scrollTop();

jQuery scrollTop() Method Example 1

Below example shows the position of the scrollbar from the top-

<script type="text/javascript">
         $(document).ready(function(){ 
              $( "#scrollTopExample" ).on( "click", function(event) {             
                  var x = $("#myDiv").scrollTop();       
                   alert(x+"px");            
              });
               
          });
</script>

Try it »

jQuery scrollTop Method Syntax 2 – Set The Position

Syntax to set position of the scrollbar from top –

$(selector).scrollTop(position);

position(px): Pixel position to be scrolled.

jQuery scrollTop() Method Example 2

Here is an example which will set position of scrollbar from top –

<script type="text/javascript">
         $(document).ready(function(){ 
              $( "#scrollTopExample" ).on( "click", function(event) {             
                $("#myDiv").scrollTop(100);               
              });
               
          });
</script>

Try it »

The above example will produce output something like this –

jQuery scrollTop Method Example


User Ans/Comments

📖 Read More


Advertisements