根据内容宽度和高度调整弹出窗口的大小
我有一个不同大小的 html 页面。根据条件我隐藏了一些内容。 如何使用javascript获取正文的宽度和高度,以便我可以根据页面内容的数量动态调整窗口大小。 谢谢...
I've an html page with varying size. Based on the conditions i hide some contents.
How can i get the width and height of the body with javascript, so that i can resize the window size dynamically based on the amount of contents of the page.
Thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,document.body.offsetWidth 和 document.body.offsetHeight 应该可以满足您的要求。请记住,设置窗口大小将与视口大小不同,具体取决于您是否有状态栏等。
哦,您正在寻找视口大小。这是指向网站的链接,解释了如何来做到这一点跨浏览器。
Well, document.body.offsetWidth and document.body.offsetHeight should do the trick for you. Remember that setting the window size is going to be different from the viewport size, depending on whether you have statusbar, etc.
Oh, you are looking for the viewport size. Here is a link to a site that explains how to do that cross-browser.
我会小心调整浏览器窗口的大小,因为它很快就会变得烦人(特别是如果用户直接打开页面而不是弹出窗口)。如果用户希望内容周围有一点空间以便于阅读怎么办?在大多数浏览器中,您还可以关闭某些 Javascript 效果,例如移动窗口和调整窗口大小 - 它们甚至可能默认关闭。
icabod 将内容包装在 div 中的想法至少在高度上应该可以正常工作。如果宽度仍然显示当前窗口的整个宽度,请尝试添加
float:left
,它将适合内容的宽度。一个不错的解决方案是使用灯箱脚本来显示内容。我不知道有什么可以临时调整 HTML/iframed 内容的大小,但值得研究一下。
I'd be careful resizing browser windows because it can very quickly get annoying (particularly if the user opens the page directly and not as a pop-up). What if a user wants a bit of space around the content to make it easier to read? In most browsers you can also turn off certain Javascript effects like moving and resizing windows - they may even be off by default.
icabod's idea of wrapping the content in a div should work fine at least for the height. If the width still shows the entire width of the current window then try adding
float:left
and it will fit to the width of the content.A nice solution would be to use a lightbox script to display the content. I don't know of any off-hand that would resize to HTML/iframed content, but it's worth looking into.