简体   繁体   English

当div滚动时如何调整宽度?

[英]How to adapt width when a div is scrolling?

I have a div with width: 400px; 我有一个width: 400px;为div的div width: 400px; and overflow: auto . overflow: auto Is it possible to adapt the width of the div when the scrollbar is displayed ? 显示滚动条时是否可以调整div的宽度?

See example : here (resize the "Result" box up and down) 参见示例: here (向上和向下调整“结果”框)

I'd prefer a css solution. 我更喜欢css解决方案。

You can use: 您可以使用:

min-width: size;

on the container css. 在容器css上。 Where size is 400px + scrollbar width. 其中大小为400px +滚动条宽度。

Take a look at demo . 看看演示

Edit 编辑

I can't find how make it possibile with css only, sorry. 抱歉,我无法找到如何使用css。
But checking if your div has scrollbar enabled, you can apply min-width attribute and it work. 但是检查你的div是否启用了滚动条,你可以应用min-width属性并且它可以工作。

Test here and let me know. 在这里测试并告诉我。

In your case, I think you have to do the hasScrollBar() test anyway happens something, such as window-resize or something else. 在你的情况下,我认为你必须做hasScrollBar()测试无论如何发生的事情,如窗口调整大小或其他东西。

This example works on event window resize. 此示例适用于事件窗口调整大小。

Use a percentage-based width, which adapts to scrollbars automatically. 使用基于百分比的宽度,自动适应滚动条。 390/400 is 97.5%, this should do: 390/400是97.5%,这应该做:

width: 97.5%;

Here's your fixed fiddle: http://jsfiddle.net/yzngV/7/ 这是你固定的小提琴: http//jsfiddle.net/yzngV/7/

You could try using vw and vh values. 您可以尝试使用vwvh值。 More info here . 更多信息在这里

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

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