Okay I'm using jQuery and currently getting max value of scrollbar doing this:
var $body = $('body');
$body.scrollLeft(99999); // will give me the max value since I've overshot
var max_value = $body.scrollLeft(); // returns 300
$('body').scrollLeft(0);
When I try this: $body[0].scrollWidth
I just get the actual width of the contents - 1580
I'm thinking there has to be a better way I'm forgetting.
EDIT
To clarify, I also tried $(window).width()
and $(document).width()
which gave me 1280 and 1580, respectively.
You can calculate the maximum value of
element.scrollLeft
with:Note that there could be some browser specific quirks that I'm not aware of.
You can use
$(document).width()
to get the full width of your document, and$(window).width()
to get the width of the window/viewport.http://api.jquery.com/width/
First of all there is a native property that is implemented in mozilla and only mozilla
scrollLeftMax
(alsoscrollTopMax
). look here: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollLeftMaxHere a polyfill i wrote that will make that property available for IE8+, and all the other browsers. Just add it at the top of your js file or code.
Here the polyfill code go:
use example:
The support here depend on
defineProperties()
support. which is IE8+ (for IE8 the method is supported for only DOM elements which is the case for our polyfill use and methods).Look here for the whole list of supported browsers: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#compatNote_1
mostly that will suffice.
If not you can add separate functions directly. and you get a support for IE6+ and all other browsers. (now it will depend on in operator support. which happen to be IE6+)
Here an example i choosed to add just an 'i' to the end for the name.
scrollLeftMaxi()
andscrollTopMaxi()
use example :
The code above create the properties the Element prototype and assign the functions we defined. When called
scrollLeftMaxi()
. The prototype chain get traversed, until it Get toElement.prototype
. Where it will find the property. Know that following the prototype chain. And how the properties are checked. If having two properties of same name at different place in the chain. Unexpected behavior is just to be expected. That's why a new name asscrollLeftMaxi
is suited. (if i kept the same as the native mozilla one, the native one will not get override, and they are in two different place in the chain, and the native one take precedence. and the native is not of function type. An attribute that have a getter behind it, just like we did with the polyfill above, if i call it as a function. An error will trigger, saying it's not a function. and so without changing the name we will have this problem with mozilla. That was for an example).Look here how getter works if you like : https://www.hongkiat.com/blog/getters-setters-javascript/
here is a fiddle test, it show that we get the same result as the native property in mozilla (make sure to test within mozilla)
What about using that with jquery:
AFAIK you have to calculate the maximum scroll value yourself, it is the difference between the parent/container's width and the child/content width.
An example on how to do that with jQuery:
HTML:
CSS:
JS:
In your case window is the parent/container and document the child/content.
Here is a JSFiddle with this example: http://jsfiddle.net/yP3wW/
Jquery (>1.9.1): The maximum value
scrollLeft
can be is: