Open In App
Related Articles

jQuery scrollLeft() Method

Improve Article
Improve
Save Article
Save
Like Article
Like

The scrollLeft() method is an inbuilt method in jQuery used to return or set the horizontal position of the scroll bar.

Syntax:

$(selector).scrollLeft(position)

Parameters: This method accepts single parameter position which is optional. It is used to specify horizontal scrollbar position in pixels.

Return Value: This method returns the position of the scrollbar.

The below example illustrates the scrollLeft() method in jQuery.

Example 1:

html




<!DOCTYPE html>
<html>
 
<head>
    <title>scrollLeft method</title>
    <script src=
    </script>
 
    <!-- jQuery code to show the working of this method -->
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                alert($("div").scrollLeft() + " px");
            });
        });
    </script>
    <style>
        div {
            border: 1px solid black;
            width: 140px;
            height: 120px;
            overflow: auto
        }
    </style>
</head>
 
<body>
    <center>
        <div>
            welcome to GeeksforGeeks!. Welcome to GeeksforGeeks1.welcome
            to GeeksforGeeks!. Welcome to GeeksforGeeks1. Welcome to
            GeeksforGeeks!. Welcome to GeeksforGeeks!.welcome to
            GeeksforGeeks!. Welcome to GeeksforGeeks1. Welcome to GeeksforGeeks!.
        </div>
        <br>
        <!-- click on this button to get the position of the scrollbar -->
        <button>Click Here!</button>
    </center>
 
</body>
 
</html>


Output:

jquery-31

Example 2:

html




<!DOCTYPE html>
<html>
 
<head>
    <title>scrollLeft method</title>
    <script src=
    </script>
 
    <!-- jQuery code to show the working of this method -->
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $("div").scrollLeft(100);
            });
        });
    </script>
    <style>
        div {
            border: 1px solid black;
            width: 140px;
            height: 120px;
            overflow: auto
        }
    </style>
</head>
 
<body>
    <center>
        <div>
            welcome to GeeksforGeeks!. Welcome to GeeksforGeeks1.welcome
            to GeeksforGeeks!. Welcome to GeeksforGeeks1. Welcome to
            GeeksforGeeks!. Welcome to GeeksforGeeks!.welcome to
            GeeksforGeeks!. Welcome to GeeksforGeeks1. Welcome to
            GeeksforGeeks!.
        </div>
        <br>
 
        <!-- click on this button to get the position
            of the scrollbar -->
        <button>Click Here!</button>
    </center>
 
</body>
 
</html>


Output: After clicking on the button position shown by the arrow.

jquery-33


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 10 Jul, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials