Hello Readers,
scrollLeft() is the jquery method which is used to return the scrollbar position to horizontally for the selected or matched elements in jquery. If the scrollbar is at the end of Left side it returns the position zero. It is also used to set the position inside the scrollbar.
Syntax :
$(selector).scrollLeft()
In the above syntax we do not pass any position. It is used as default.
$(selector).scrollLeft(position)
In the above syntax we pass the position inside the scrollbar() method.
Code Example :
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert($("div").scrollLeft() + " px");
});
});
</script>
</head>
<body>
<div style="border:1px solid black;width:100px;height:130px;overflow:auto">
This is a div.
</div><br>
<button>Horizontal scrollbar</button>
<p>This ia s paragraph</p>
</body>
</html>
In the above code we use the scrollbar method and return the horizontal position of the scrollbar which is zero and if we set the position then scrollbar move upto to that position.
0 Comment(s)