jQuery scrollLeft Method


jQuery .scrollLeft Method : is used to get or set the horizontal scrollbar position of the selected element. .scrollLeft() is used to set or get the scrollbar position. If you want to set position of scrollbar pass value in pixel as input parameter.
We are going to explain both example with demo and example.


jQuery scrollLeft Method Syntax 1 – Get The Position

Syntax for scrollLeft Method –

$(selector).scrollLeft();

jQuery scrollLeft() Method Example 1

Below example shows the scrollLfet method get scrollbar position method –

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

Try it »

jQuery scrollLeft Method 2 – Set The Position

Syntax for set position –

$(selector).scrollleft(position);

position(px): Pixel position to be scrolled.

jQuery scrollLeft() Method Example 2

Below example will set position 100 from left –

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

Try it »

Here is output of the example –

jQuery scrollLeft Method


Advertisements

Add Comment

📖 Read More