如何获取scrollLeft的最大值? [英] How do I get the max value of scrollLeft?
问题描述
好吧我正在使用jQuery并且当前获得滚动条的最大值这样做:
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);
当我尝试这个: $ body [0] .scrollWidth
我只是得到了内容的实际宽度 - 1580
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.
编辑
为了澄清,我还尝试了 $(窗口).width()
和 $(文件).width()
分别给了我1280和1580.
EDIT
To clarify, I also tried $(window).width()
and $(document).width()
which gave me 1280 and 1580, respectively.
推荐答案
您可以使用以下公式计算 element.scrollLeft
的最大值:
You can calculate the maximum value of element.scrollLeft
with:
var maxScrollLeft = element.scrollWidth - element.clientWidth;
请注意,可能存在一些我不知道的浏览器特定怪癖。
Note that there could be some browser specific quirks that I'm not aware of.
这篇关于如何获取scrollLeft的最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!