【发布时间】:2011-02-05 11:57:17
【问题描述】:
我正在使用 jquery 编写一个站点,该站点重复调用 $(window).width() 和 $(window).height() 以根据视口大小定位和调整元素的大小。
在故障排除中,我发现在未调整视口大小时重复调用上述 jquery 函数时,我得到的视口大小报告略有不同。
想知道是否有任何人知道何时发生这种情况的特殊情况,或者是否就是这样。报告的大小差异是 20 像素或更小,它似乎。它发生在 Mac OS X 10.6.2 上的 Safari 4.0.4、Firefox 3.6.2 和 Chrome 5.0.342.7 beta 中。我还没有测试其他浏览器,因为它似乎不是特定于浏览器的。我也无法弄清楚差异取决于什么,如果不是视口大小,是否还有其他因素导致结果不同?
任何见解将不胜感激。
更新:
改变的不是$(window).width() 和$(window).height() 的值。这是我用来存储上述值的变量的值。
影响值的不是滚动条,当变量值改变时不会出现滚动条。这是我将值存储在变量中的代码(我这样做只是为了使它们更短)。
(所有这些都在$(document).ready()内)
//最初声明变量对.ready()中的其他函数可见
var windowWidth = $(window).width(); //retrieve current window width
var windowHeight = $(window).height(); //retrieve current window height
var documentWidth = $(document).width(); //retrieve current document width
var documentHeight = $(document).height(); //retrieve current document height
var vScrollPosition = $(document).scrollTop(); //retrieve the document scroll ToP position
var hScrollPosition = $(document).scrollLeft(); //retrieve the document scroll Left position
function onm_window_parameters(){ //called on viewer reload, screen resize or scroll
windowWidth = $(window).width(); //retrieve current window width
windowHeight = $(window).height(); //retrieve current window height
documentWidth = $(document).width(); //retrieve current document width
documentHeight = $(document).height(); //retrieve current document height
vScrollPosition = $(document).scrollTop(); //retrieve the document scroll ToP position
hScrollPosition = $(document).scrollLeft(); //retrieve the document scroll Left position
}; //end function onm_window_parameters()
我插入了一条警告语句,以根据它们应该持有的值来探测上面的变量。 $(item).param() 值保持一致,但我的变量因我无法弄清楚的原因而发生变化。
我一直在寻找我的代码可能会改变相关变量值的地方,而不是仅仅检索它们的设定值而找不到任何地方。如果有可能,我可以将整个 shebang 发布到某个地方。
【问题讨论】:
标签: jquery resize height width viewport