我正在使用jquery编写一个站点,它反复调用$(window).width()$(window).height()来根据视口大小定位和调整元素的大小.

In troubleshooting I discovered that I am getting slightly different viewport size reports in repeated calls to the above jquery functions when the viewport is not resized.

想知道是否有任何特殊情况下,任何人都知道这种情况发生,或者如果这只是它的方式.据报道,大小的差异似乎是20px或更小.它发生在Mac OS X 10.6.2上的Safari 4.0.4、Firefox 3.6.2和Chrome 5.0.342.7测试版中.我还没有测试其他浏览器,因为它似乎不是特定于浏览器的.我也无法找出差异取决于什么,如果不是视口大小,是否还有其他因素导致结果不同?

如有任何见解,将不胜感激.


update:

It is not the values of $(window).width() and $(window).height() that are changing. It's the values of the variables I am using to store the values of the above.

影响值的不是滚动条,当变量值更改时不会出现滚动条.下面是我在变量中存储值的代码(我这样做只是为了让它们更短).

(这一切都在$(document).ready()以内)

//initially declare the variables to be visible to otehr functions within .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()

I inserted an alert statement to probe the variables above against the values they are supposed to be holding. The $(item).param() values stay consistent, but my variables change for reasons I can't figure out.

我一直在寻找我的代码可能会改变相关变量值的地方,而不是仅仅检索它们的设置值,却什么都找不到.如果有可能的话,我可以把整个shebang发到某个地方.

推荐答案

I think what you're seeing is the hiding and showing of scrollbars. Here's a quick demo showing the width change.

As an aside: do you need to poll constantly? You might be able to optimize your code to run on the resize event, like this:

$(window).resize(function() {
  //update stuff
});

Jquery相关问答推荐

在Click事件上将焦点设置为Kendo UI网格列筛选器文本框

使用 howler.js 中的变量播放多种声音

为什么函数没有进入

jQuery .append() 创建两个元素而不是一个

使用 Bootstrap 3 下拉菜单作为上下文菜单

Bootstrap 的工具提示在悬停时将表格单元格向右移动一点

使用 jQuery / javascript 测试链接是否是外部的?

提醒未保存的表单更改

如何使用 jQuery Select 所有复选框?

在 contenteditable div 中的插入符号处插入 html

为什么 jquery fadeIn() 不能与 .html() 一起使用?

为什么要两次声明 jQuery?

即使通过Javascript代码判断,如何触发复选框单击事件?

DataTables:无法读取未定义的属性长度

如何签入用户已在 Google recaptcha 中选中复选框的 js?

如何使用 JSON、jQuery 将一组复杂对象发布到 ASP.NET MVC 控制器?

Url.Action 在我的 url 中放了一个 &,我该如何解决这个问题?

如何在 JavaScript / jQuery 中获取对象的属性?

通过 javascript/jquery 删除/截断前导零

如何使用 jquery 使下拉列表只读?