简体   繁体   English

jQuery获取文档宽度

[英]jquery get document width

Im running in to a weird issue with getting the width of the document. 我在获取文档的宽度时遇到了一个奇怪的问题。 Im using $(document).width() to get the width both on $(window).load and $(window).resize , my problem occurs when the browser is open full screen then resized (shrank) to a width where the content requires you to scroll horizontally to see all the content. 我使用$(document).width()来获取$(window).load$(window).resize的宽度,当浏览器全屏打开然后调整大小(缩小)到内容要求您水平滚动以查看所有内容。 At the point in which you need to scroll my header and footer divs just end. 在您需要滚动我的页眉和页脚div的那一点刚刚结束。 I would like them to go 100% of the document width. 我希望他们使用100%的文档宽度。 The problem fixes itself when i refresh the page. 当我刷新页面时,问题会自行解决。 Is there a solution that allows a browser to shrink when resizing the window and updates the header and footer divs? 有没有一种解决方案可以让浏览器在调整窗口大小时缩小并更新页眉和页脚div? This is what i have so far and its not working. 这是我到目前为止所无法使用的。

$(window).load(function() {
    resize_me();
});
$(window).resize(function() {
    resize_me();
});
function resize_me() {
    var doc_width = $(document).width();
    $('#header').width(doc_width);
    $('#footer').width(doc_width);
    console.log(doc_width);
}

try window.innerWidth , it should get you the size of the viewport. 尝试window.innerWidth ,它应该为您提供视口的大小。

No jQuery needed. 无需jQuery。

and honestly, why not just set their widths to 100% with css ??? 老实说,为什么不使用CSS将其宽度设置为100%

#header, #footer {
    width:100%;
}

If I understand what you are saying correctly you could try the following in your stylesheet, which would be much more efficient than using JS. 如果我理解正确的话,可以在样式表中尝试以下方法,这比使用JS效率更高。

#header, #footer{
    width : 100%;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM