简体   繁体   English

浏览器调整大小时如何正确调整div大小?

[英]How to resize div properly when browser resizes?

Assume I have the following table: 假设我有下表:

<div style="padding:10px">
<table style="width:100%">
<tr><td style="width:10%">Jordan Roger</td><td style="width:100%"><div width="200px" style="background:red"></div></td></tr>
</table>
</div>

How can I make it with javascript such that that 200px div will always fill the space up to the right side of the screen (honoring the 10px padding) whenever the browser is resized? 如何使用Javascript进行调整,以便无论何时调整浏览器大小时,200px的div始终会填充屏幕右侧的空间(保留10px的填充)? (height can stay the same, width is priority. (高度可以保持不变,宽度优先。

Note: Percentages are not an option for the div. 注意:百分比不是div的选项。 Usually I would use a percentage like 100%, however this is not an option as I can only set the div's width to a specific value in px, not percentage with a "thediv.setSize(width, height)". 通常我会使用100%之类的百分比,但是这不是一个选择,因为我只能将div的宽度设置为以px为单位的特定值,而不能设置为带有“ thediv.setSize(width,height)”的百分比。

If possible I was thinking about the following as a potential means of doing this, but do not know how to properly utilize it as the 200px div is not going to be taking up the whole height and width of the browser as it is located in a table. 如果可能的话,我正在考虑将以下内容作为执行此操作的一种潜在方法,但不知道如何正确利用它,因为200px div不会占据浏览器的整个高度和宽度,因为它位于表。

$(window).resize(function() 
{    
    mydiv.setSize($(document).width(), $(document).height()));   
});

If there is some "right" way I should be using this... 如果有某种“正确”的方式,我应该使用这种方式...

You'll have to use a width percentage - ie a "relative" width. 您必须使用宽度百分比-即“相对”宽度。


Edited to reflect changed requirements. 编辑以反映变化的需求。

Why not set the style with JS like here? 为什么不使用JS这样设置样式? How to set width of a div in percent in JavaScript? 如何在JavaScript中以百分比设置div的宽度?

Alternatively, you might be able to bind a listener to: http://api.jquery.com/resize/ , that checks the width of the page, and calls that setSize function 另外,您也许可以将侦听器绑定到: http : //api.jquery.com/resize/ ,它检查页面的宽度,并调用setSize函数。

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

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