jQuery scrollLeft() Method
Last Updated :
31 Jul, 2024
Improve
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:
<!DOCTYPE html>
<html>
<head>
<title>scrollLeft method</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</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>
43
1
2
<html>
3
4
<head>
5
<title>scrollLeft method</title>
6
<script src=
7
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
8
</script>
9
10
<!-- jQuery code to show the working of this method -->
11
<script>
12
$(document).ready(function () {
13
$("button").click(function () {
14
alert($("div").scrollLeft() + " px");
15
});
16
});
17
</script>
18
<style>
19
div {
20
border: 1px solid black;
21
width: 140px;
22
height: 120px;
23
overflow: auto
24
}
25
</style>
26
</head>
27
28
<body>
29
<center>
30
<div>
31
welcome to GeeksforGeeks!. Welcome to GeeksforGeeks1.welcome
32
to GeeksforGeeks!. Welcome to GeeksforGeeks1. Welcome to
33
GeeksforGeeks!. Welcome to GeeksforGeeks!.welcome to
34
GeeksforGeeks!. Welcome to GeeksforGeeks1. Welcome to GeeksforGeeks!.
35
</div>
36
<br>
37
<!-- click on this button to get the position of the scrollbar -->
38
<button>Click Here!</button>
39
</center>
40
41
</body>
42
43
</html>
Output:
Example 2:
<!DOCTYPE html>
<html>
<head>
<title>scrollLeft method</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</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>
46
1
2
<html>
3
4
<head>
5
<title>scrollLeft method</title>
6
<script src=
7
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
8
</script>
9
10
<!-- jQuery code to show the working of this method -->
11
<script>
12
$(document).ready(function () {
13
$("button").click(function () {
14
$("div").scrollLeft(100);
15
});
16
});
17
</script>
18
<style>
19
div {
20
border: 1px solid black;
21
width: 140px;
22
height: 120px;
23
overflow: auto
24
}
25
</style>
26
</head>
27
28
<body>
29
<center>
30
<div>
31
welcome to GeeksforGeeks!. Welcome to GeeksforGeeks1.welcome
32
to GeeksforGeeks!. Welcome to GeeksforGeeks1. Welcome to
33
GeeksforGeeks!. Welcome to GeeksforGeeks!.welcome to
34
GeeksforGeeks!. Welcome to GeeksforGeeks1. Welcome to
35
GeeksforGeeks!.
36
</div>
37
<br>
38
39
<!-- click on this button to get the position
40
of the scrollbar -->
41
<button>Click Here!</button>
42
</center>
43
44
</body>
45
46
</html>
Output: After clicking on the button position shown by the arrow.