如何获得scrollLeft的最大值?
好吧,我正在使用 jQuery,目前正在获取滚动条的最大值:
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
我在想一定有更好的方法我忘记了。
编辑 为了澄清这一点,我还尝试了 $(window).width()
和 $(document).width()
,它们分别给了我 1280 和 1580。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用以下方法计算
element.scrollLeft
的最大值:请注意,可能存在一些我不知道的浏览器特定的怪癖。
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.
首先,有一个本机属性在 mozilla 中实现,并且仅在 mozilla
scrollLeftMax
(也称为scrollTopMax
)中实现。看这里: https://developer.mozilla.org/en -US/docs/Web/API/Element/scrollLeftMax这里是我编写的一个polyfill,它将使得该属性可用于 IE8+,并且所有其他浏览器。只需将其添加到 js 文件或代码的顶部即可。
这里是polyfill代码:
使用示例:
这里的支持依赖于
defineProperties()
支持。这是 IE8+(对于 IE8,该方法仅支持 DOM 元素,这就是我们的 polyfill 使用和方法的情况)。在这里查看支持的浏览器的完整列表:
https://developer.mozilla。 org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#compatNote_1
大部分就足够了。
如果您无法直接添加单独的功能。并且您可以获得对 IE6+ 和所有其他浏览器的支持。 (现在它将取决于运营商支持,恰好是 IE6+)
这里是一个例子,我选择在名称末尾添加一个“i”。
scrollLeftMaxi()
和scrollTopMaxi()
使用示例:
上面的代码创建 Element 原型的属性并分配我们定义的函数。当调用
scrollLeftMaxi()
时。遍历原型链,直到到达Element.prototype
。它将在哪里找到该属性。知道下面的原型链。以及如何检查属性。如果在链中的不同位置有两个同名的属性。意外的行为只是意料之中的。这就是为什么适合使用新名称scrollLeftMaxi
的原因。 (如果我与本机 mozilla 保持相同,则本机将不会被覆盖,并且它们位于链中的两个不同位置,并且本机优先。并且本机不是函数类型。一种属性它后面有一个 getter,就像我们对上面的 polyfill 所做的那样,如果我将其作为函数调用,则会触发错误,说明它不是函数,因此在不更改名称的情况下,我们将遇到 mozilla 的问题。举个例子)。如果您愿意,请看这里 getter 的工作原理:
https://www.hongkiat.com/blog/getters-setters-javascript/< /a>
这里是一个小提琴测试,它表明我们得到了与 mozilla 中的本机属性相同的结果(确保在 mozilla 中进行测试)
将其与 jquery 一起使用怎么样:
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 you cannot add separate functions directly. and you get a support for IE6+ and all other browsers. (now it will depend on in operator support, which happens to be IE6+)
Here an example I chose 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你必须自己计算最大滚动值,它是父/容器宽度和子/内容宽度之间的差异。
有关如何使用 jQuery 执行此操作的示例:
HTML:
CSS:
JS:
在您的情况下,窗口 是父/容器,文档 是子/内容。
这是带有以下示例的 JSFiddle: http://jsfiddle.net/yP3wW/
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/
简单的解决方案是
Simple solution is
Jquery (>1.9.1):
scrollLeft
的最大值为:Jquery (>1.9.1): The maximum value
scrollLeft
can be is:您可以使用
$(document).width()
获取文档的完整宽度,使用$(window).width()
获取窗口的宽度/视口。http://api.jquery.com/width/
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/
我认为你可以使用这样的东西:
前一行代码将获取其中一个子元素的宽度并将其乘以子元素的数量,最后你需要减去元素的宽度减去偏移量
I think that you could use something like this:
The previous line of code is going to get the width of one of the children and multiply this with by the number of chilren, finally you need to substract the width of the element minus an offteset