简体   繁体   English

自动调整div的大小

[英]Automatically resizing div

This is another of the infinite questions about how to get div sizing to behave, but conversely I haven't been able to find a sufficiently similar question to get my particular case working. 这是关于如何使尺寸变化表现出来的另一个无限问题,但是相反地,我还没有找到足够类似的问题来使我的特殊情况发挥作用。

 function update() { $('#container .body-text').bigText({verticalAlign:'top',whiteSpace:'pre'}); } function doresize(form) { var container = $('#container')[0]; container.style.height = form.height.value + 'px'; setTimeout(500, update); } function dotext(form) { $('#container .body-text').text(form.text.value); update(); } update(); 
 .container { float: left; border: solid 1px black; position: relative; margin-left: 5px; width: 20%; height: 200px; padding: 2px; } .header { width: 100%; height: 64px; background-color: blue; } .body { width: 100%; height: 100%; max-height: calc(100% - 64px); background-color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <script src="https://rawgithub.com/DanielHoffmann/jquery-bigtext/master/jquery-bigtext.js"></script> <form name="setup"> <label>Set height: <input type="number" name="height" value="200" /></label> <button type="button" onclick="doresize(this.form)">Resize</button><br /> <label>Set text: <textarea name="text">Multi-Line Text</textarea></label> <button type="button" onclick="dotext(this.form)">Change</button><br /> </form> <br /> <div id="container" class="container" onload="update()"> <div class="header"></div> <div class="body"><div class="body-text">Multi-Line Text</div></div> </div> 

The above snippet shows a fairly basic layout with a container div with a width and height that are controlled by outside forces. 上面的代码段显示了一个相当基本的布局,其中一个容器div的宽度和高度由外部力控制。 (In the real code this is controlled by the browser window resizing, but I've put in a basic form height adjustment here for demonstration purposes.) (在实际代码中,这受浏览器窗口大小调整的控制,但出于演示目的,我在此处进行了基本的表单高度调整。)

What I'm trying to achieve with this is the following: 我正在尝试实现以下目标:

  1. Both internal divs fill to 100% width of their parent. 两个内部div都填充为其父级的100%宽度。
  2. The top (blue) div has a fixed height. 顶部(蓝色)div的高度固定。
  3. The bottom (green) div will use the remaining space in the parent by default. 默认情况下,底部(绿色)div将使用父级中的剩余空间。
  4. However, after a bit of existing JS code resizes the font size of the text to best-fit this space (both width and height)... 但是,在一些现有的JS代码调整了文本的字体大小以使其最适合该空间(宽度和高度)之后,...
  5. If the container is tall enough and the text is short enough that there's extra space below, then the green div should vertically shrink to fit its text and both the blue and green divs should then appear vertically centred in the container div. 如果容器足够高且文本足够短,以至下面有多余的空间,则绿色div应该垂直缩小以适合其文本,然后蓝色和绿色div都应垂直出现在容器div的中心。
  6. If the container size or the text changes, repeat the process of fitting the text and recentering. 如果容器大小或文本更改,请重复进行文本拟合和重新居中的过程。

I know how to do most of the pieces individually, I'm just not sure how to put it all together, and whether step 5 is possible with CSS or whether it requires JS. 我知道如何单独处理大部分内容,我不确定如何将所有内容放在一起,以及CSS是否可以执行步骤5或是否需要JS。 (I've tried adding the vertical centering with CSS via the absolute-translate-50% trick, which works great at tall container sizes or text that is wider than it is tall, but not the other way around -- the text overflows the green div because the height is not fixed so can't be taken into account by the font sizing script.) (我已经尝试通过absolute-translate-50%技巧在CSS上添加垂直居中,这在高容器尺寸或宽于高的文本上效果很好,但反之则不行-文本溢出绿色div,因为高度不固定,因此字体大小调整脚本无法将其考虑在内。)

I'm ok with rearranging or inserting additional divs if this is required. 如果需要的话,我可以重新排列或插入其他div。


EDIT : the following snippet inspired by Shadi's answer seems to do the trick: 编辑 :以下来自Shadi的答案的代码片段似乎可以解决问题

 function update() { var text = $('#container .body-text'); text.parent().parent().css('height', '100%'); text.parent().css('height', '100%'); text.bigText({verticalAlign:'top',whiteSpace:'pre'}); text.parent().css('height', text.height()); text.parent().parent().css('height', text.height() + 64); } function doresize(form) { $('#container').css('height', form.height.value); update(); } function dotext(form) { $('#container .body-text').text(form.text.value); update(); } update(); 
 .container { float: left; border: solid 1px black; margin-left: 5px; width: 20%; height: 200px; padding: 2px; } .subcontainer { position: relative; width: 100%; height: 100%; top: 50%; transform: translateY(-50%); } .header { width: 100%; height: 64px; background-color: blue; } .body { width: 100%; height: 100%; max-height: calc(100% - 64px); background-color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <script src="https://rawgithub.com/DanielHoffmann/jquery-bigtext/master/jquery-bigtext.js"></script> <form name="setup"> <label>Set height: <input type="number" name="height" value="200" /></label> <button type="button" onclick="doresize(this.form)">Resize</button><br /> <label>Set text: <textarea name="text">Multi-Line Text</textarea></label> <button type="button" onclick="dotext(this.form)">Change</button><br /> </form> <br /> <div id="container" class="container"> <div class="subcontainer"> <div class="header"></div> <div class="body"><div class="body-text">Multi-Line Text</div></div> </div> </div> 

Try values between 80 and 300 to see it in action. 尝试在80到300之间的值进行查看。

I have tried this in a code, please would you try it and see if it fits your need, the following code will make the green div fullfil the remining container height, and then after calcuating the text size and width it shrinks to fit the new text height 我已经在代码中尝试过了,请您尝试一下,看看它是否满足您的需要,以下代码将使绿色div完全填充提醒容器的高度,然后在计算文本大小和宽度后将其缩小以适合新的文字高度

function update() {
        $('#container').css('height', '200px' /*this would be dynamic based on the container height*/);
        $('#container .body-text').bigText({ verticalAlign: 'top', whiteSpace: 'pre' });
        $('#container').css('height', $('#bodytxt').height() + 64 /*this is the header height - blue div*/);
    }

You need also to add an id for your bodytext div: 您还需要为您的bodytext div添加一个ID:

<div id="bodytxt" class="body-text">Multi-Line Text</div>

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

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