jQuery API

.scrollLeft()

Contents:

.scrollLeft() Returns: Integer

Description: Get the current horizontal position of the scroll bar for the first element in the set of matched elements.

  • version added: 1.2.6.scrollLeft()

The horizontal scroll position is the same as the number of pixels that are hidden from view above the scrollable area. If the scroll bar is at the very left, or if the element is not scrollable, this number will be 0.

Example:

Get the scrollLeft of a paragraph.

<!DOCTYPE html>
<html>
<head>
  <style>
    p { margin:10px;padding:5px;border:2px solid #666; }
    </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>Hello</p><p></p>
<script>var p = $("p:first");
			$("p:last").text( "scrollLeft:" + p.scrollLeft() );

			</script>

</body>
</html>

Demo:

.scrollLeft( value ) Returns: jQuery

Description: Set the current horizontal position of the scroll bar for each of the set of matched elements.

  • version added: 1.2.6.scrollLeft( value )

    valueAn integer indicating the new position to set the scroll bar to.

The horizontal scroll position is the same as the number of pixels that are hidden from view above the scrollable area. Setting the scrollLeft positions the horizontal scroll of each matched element.

Example:

Set the scrollLeft of a div.

<!DOCTYPE html>
<html>
<head>
  <style>
  div.demo {
  background:#CCCCCC none repeat scroll 0 0;
  border:3px solid #666666;
  margin:5px;
  padding:5px;
  position:relative;
  width:200px;
  height:100px;
  overflow:auto;
  }
  p { margin:10px;padding:5px;border:2px solid #666;width:1000px;height:1000px; }
	</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div class="demo"><h1>lalala</h1><p>Hello</p></div>
<script>$("div.demo").scrollLeft(300);
</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .scrollLeft() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .scrollLeft()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • Wirthdesign
    Can you add duration to this to make it a bit smoother?
  • You can use the .animate() method. http://api.jquery.com/animate/
    .animate({scrollLeft: someNumber}, 'slow');
  • Igor
    Fuqi7758521:
    If you use negative numbers, it will set you at position "0" because there is no scroll bar left in browser below "0".

    Lpyedge:
    Inicially it will be 0 unless you change it before the document got ready. If you got a site that is wider then the user screen and you need the user to use the center part of your site first, then you can you that code to set it as much as you need (some prev calculating will be needed I guess).

    Hope can help you.
  • Fuqi7758521
    it can be negative number?
  • Lpyedge
    $(window).scrollLeft() is allways be num 0???
  • Tricky
    Documentation should read "The horizontal scroll position is the same as the number of pixels that are hidden from view TO THE LEFT OF the scrollable area."