Chrome 中的 JQuery 窗口宽度不正确

标签 jquery google-chrome window width

我创建了一个插件,我已将其简化为插件的骨架(即没有功能,只有结构)来调试此问题,但它仍然存在。它处理调整 div 的大小并基于文档的宽度。

问题是文档报告的宽度比实际宽度减少了 17px,因此无法正确调整 div 的大小。

我已发出警报以找出问题发生的位置: 在“准备好”功能之前:1780px(正确宽度) 在“ready”函数内部:1763px 插件内:1763px (从“document.width”和“$(document).width()”返回的所有值具有相同的结果)

这种情况只发生在 Chrome 中,即使插件减少到几乎没有,这种情况仍然存在。

对于为什么会发生这种情况有什么想法吗?

最佳答案

取自jQuery documentation :

While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code. When using scripts that rely on the value of CSS style properties, it's important to reference external stylesheets or embed style elements before referencing the scripts.

In cases where code relies on loaded assets (for example, if the dimensions of an image are required), the code should be placed in a handler for the load event instead.

你可能应该使用

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

作为触发事件

关于Chrome 中的 JQuery 窗口宽度不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/887931/

相关文章:

javascript - 进度加载栏在 chrome 和 IE 中不起作用

jquery - CSS 动画一次一行

c++ - Winapi:屏幕截图未显示在窗口上

c++ - 将窗口位置限制在桌面工作区

php - 在脚本中使用 php

javascript - jQuery backstretch 图像自动移动和返回

python - 我在哪里可以找到所有可用的 ChromeOptions with selenium 的列表?

javascript - 绕过 Chrome 中的 CORS 问题

google-chrome - Chrome 桌面上的 "Articles for you"建议(Chrome 内容建议)

wpf - 处理 WPF 窗口的正确方法是什么?