7

I have created a plugin which i have cut down to the bare bones of a plugin (i.e. no functionality, just the structure) to debug this issue and it still exists. It deals with resizing divs and bases this on the document's width.

The problem is that the documents reported width is reduced by 17px from its actual width and thus doesnt size the divs correctly.

I have put in alerts to find out where the problem occurs: Before "ready" function: 1780px (correct width) Inside the "ready" function: 1763px Inside the Plugin: 1763px (all values returned from "document.width" and "$(document).width()" with same results)

This is only happening within Chrome and still remains even with the plugin reduced to effectively nothing.

Any thoughts on why this is happening?

4

2 回答 2

3

取自jQuery 文档

虽然 JavaScript 提供了在页面呈现时执行代码的加载事件,但在完全接收到所有资产(例如图像)之前,不会触发此事件。在大多数情况下,只要完全构建了 DOM 层次结构,就可以运行脚本。传递给 .ready() 的处理程序保证在 DOM 准备好后执行,因此这通常是附加所有其他事件处理程序和运行其他 jQuery 代码的最佳位置。当使用依赖 CSS 样式属性值的脚本时,在引用脚本之前引用外部样式表或嵌入样式元素很重要。

如果代码依赖于加载的资产(例如,如果需要图像的尺寸),则应将代码放置在加载事件的处理程序中。

你可能应该使用

$(window).load(function() {});

作为触发事件

于 2011-03-08T10:04:06.783 回答
2

I have found that jQuery(window).width() return a wrong value in Safari and Chrome.

于 2010-01-22T14:29:42.260 回答