[英]Smooth CSS 'collapse' transition on width, padding and border
I am trying to create a transition that will effectively 'collapse' a div and floating elements that it contains, by altering the width, padding and border. 我正在尝试创建一个过渡,通过更改宽度,填充和边框来有效地“折叠”其包含的div和浮动元素。
I have done some investigation which you can see here: http://jsfiddle.net/p38b6ndb/1 我已经做了一些调查,您可以在这里查看: http : //jsfiddle.net/p38b6ndb/1
To include a sample: 包括样本:
HTML: HTML:
<div>
<div class="section">
<div class="sub-section">label</div>
<div class="sub-section">field</div>
</div>
<div class="section collapsible">
<div class="sub-section">label</div>
<div class="sub-section">field</div>
</div>
<div class="section">
<div class="sub-section">label</div>
<div class="sub-section">field</div>
</div>
</div>
CSS: CSS:
.section {
float: left;
width: 25%;
}
.section.collapsible {
opacity: 1;
transition: opacity linear 1s, width linear 1s;
}
.section.collapsible.collapsed {
width: 0;
opacity: 0;
}
.sub-section {
width: 50%;
float: left;
height: 42px;
}
The scenario: the 2nd section should collapse, with the 3rd section moving left alongside the 1st section. 场景:第二部分应折叠,第三部分应与第一部分向左移动。
The problem: if I have padding
and/or border
on the div that is being collapsed, when the width reaches a certain amount on collapse the divs wrap around, causing the height to increase, pushing subsequent elements downwards. 问题:如果要折叠的div上有
padding
和/或border
,则当折叠时宽度达到一定量时,div会回绕,导致高度增加,从而将后续元素向下推。
box-sizing: border-box
but this has a clear wrapping effect as well. box-sizing: border-box
但这也具有明显的环绕效果。 padding
and border-width
as well as the width
, but again it still wraps. padding
和border-width
以及width
,但是还是可以换行。 I could experiment with different durations but it still has a strange effect. margin-left
, but this doesn't affect the height and the margin-left
,但这不会影响高度和 How can I implement this collapse transition on divs with padded/bordered elements? 如何在具有填充/边框元素的div上实现此折叠过渡?
If an answer could be demonstrated by updating the fiddle that would be really welcome. 如果可以通过更新小提琴来证明答案,那将是非常受欢迎的。
Try to set 'padding: 0' to each .collapsed > sub-section-border-box, and of course set a transition 尝试为每个.collapsed> sub-section-border-box设置“ padding:0”,当然要设置一个过渡
.section.collapsible .sub-section-border-box {
padding: 10px;
border-width: 2px;
transition: all linear 1s;
}
.section.collapsible.collapsed .sub-section-border-box{
padding: 0;
border-width: 0;
}
So far, the best solution I have found is to transition padding and border-width to 0, but only for the left and right, not for the top and bottom. 到目前为止,我发现的最佳解决方案是将padding和border-width转换为0,但仅适用于左侧和右侧,而不适用于顶部和底部。 That way, the collapse appears only to be on the width, the divs do not change height at all.
这样,崩溃看起来只是在宽度上,div根本不改变高度。
.section.collapsible {
opacity: 1;
transition: opacity linear 1s, width linear 1s;
}
.section.collapsible.collapsed {
width: 0;
opacity: 0;
}
.section.collapsible .sub-section {
padding: 10px;
border-width: 2px;
transition: padding linear 1s, border-width linear 1s;
}
.section.collapsible.collapsed .sub-section {
padding: 10px 0;
border-width: 2px 0;
}
Demo: http://jsfiddle.net/p38b6ndb/6/ 演示: http : //jsfiddle.net/p38b6ndb/6/
This also means I can still use box-sizing: border-box
as well. 这也意味着我仍然可以使用
box-sizing: border-box
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.