Javatpoint Logo
Javatpoint Logo

jQuery scrollLeft() method

The scrollLeft() method is used to set or return the horizontal scrollbar position for the selected elements. When the scrollbar is on the left, it specifies the position 0.

To return the position: When we use the scrollLeft() method to get the position, it will return the current horizontal position of the first matched element in the set.

To set the position: When this method is used to set the position, it sets the horizontal position of the scrollbar for all matched elements.

Syntax

The scrollLeft() method accepts a parameter if we have to set the horizontal scrollbar position. Whereas, there is no parameter is required to get the position of the horizontal scrollbar.

To set horizontal scrollbar position:

To return horizontal scrollbar position:

The position parameter mentioned in the above syntax is an integer, used to specify the horizontal scrollbar position in pixels.

Now, let's see some of the illustrations to understand the use of the scrollLeft() method.

Example1

In this example, we are using the scrollLeft() method to get the position of the horizontal scrollbar.

Test it Now

Output

jQuery scrollLeft() method

After moving the scrollbar left and then clicking on the given button, an alert box will be displayed showing the current position of the horizontal scrollbar, as shown in the below screenshot -

jQuery scrollLeft() method

Example2

In this example we are setting the position of the horizontal scrollbar by using the position parameter of the scrollLeft() method. When user will click the given button, the horizontal scrollbar will set to 100px left, and the current position will be displayed in an alert box.

Test it Now

Output

jQuery scrollLeft() method

After clicking the given button, the scrollbar is set to 100px left, and the position of the horizontal scrollbar will be displayed in the alert box, as shown in the following screenshots -

jQuery scrollLeft() method
jQuery scrollLeft() method





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA