我正在使用此代码:LINK - 以水平方式指示读取位置。
我想调整这个脚本来获取不是整个文档的高度,而是仅获取我的 div 的高度之一。这是原始函数:
var getMax = function(){
return $(document).height() - $(window).height();
}
我尝试用以下代码替换上面的代码:
var getMax = function(){
var content = $("#content");
return content.height();
}
Firefox 中的一切都工作正常,但在 Chrome 中,我的水平栏立即被填满。我该如何解决这个问题?
最佳答案
您可以通过更改 $(window).load(fn)
事件中的 $(document).ready(fn)
来修复此问题。
ready
在 DOM 完全加载时触发,在这种情况下,Chrome 中的元素尚未准备好或尚未完全加载load
,而是向您保证页面中的所有资源都已加载,以便您可以安全地对元素进行操作(在您的情况下计算高度)。 This demo适用于 Firefox 和 Chrome。
来自documentation :
The
load
event is sent to an element when it and all sub-elements have been completely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object.
Here一篇文章更深入地解释了 ready
和 load
事件之间的区别。
关于javascript - 读取位置指示器 - Chrome 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32397978/