javascript - 可折叠与内容相关的扩展大小
问题描述
我使用 CSS 和 JS 编写了一个可折叠按钮,我想将一个按钮嵌套在另一个按钮中。
是否可以动态调整第一个/外部按钮的高度,以便在打开第二个/内部按钮时,第一个按钮的高度增加第二个按钮的高度?
这是我正在处理的代码
coll = document.getElementsByClassName("col");
conn = document.getElementsByClassName("con");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].setAttribute('data-id', 'con' + i);
conn[i].setAttribute('id', 'con' + i);
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var content = document.getElementById(this.getAttribute('data-id'));
if (content.style.maxHeight) {
content.style.maxHeight = null;
} else {
content.style.maxHeight = content.scrollHeight + "px";
}
});
}
.col {
cursor: help;
border: none;
outline: none;
background: none;
padding: 0;
font-size: 1em;
color: green;
border-bottom: 1px dashed;
transition: .3s;
}
.con {
padding: 0 18px;
margin: 3px 0;
max-height: 0;
overflow: hidden;
transition: .3s ease-out;
background-color: #f1f1f1;
box-shadow: 5px 5px 10px 3px inset rgba(0,0,0,0.60);
}
Does <button class=col>this</button> work?
<div class=con>
<p>Yes!</p>
And what about <button class=col>this</button>?
<div class=con>
<p>Yes, but below text is gone!</p>
</div>
<p>bye bye</p>
</div>
There is something to fix.
编辑:懒惰的解决方案
在 javascript 中替换content.scrollHeight
为高值,例如9999
.
缺点:当按钮打开时,从0到高度的过渡立即开始,并且由于要覆盖的空间很大,所以速度非常快;当按钮关闭时,过渡从最大高度(不是实际高度)开始,因此从最大高度到实际高度在视觉上没有任何反应,从而导致延迟。
coll = document.getElementsByClassName("col");
conn = document.getElementsByClassName("con");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].setAttribute('data-id', 'con' + i);
conn[i].setAttribute('id', 'con' + i);
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var content = document.getElementById(this.getAttribute('data-id'));
if (content.style.maxHeight) {
content.style.maxHeight = null;
} else {
content.style.maxHeight = 9999 + "px";
}
});
}
.col {
cursor: help;
border: none;
outline: none;
background: none;
padding: 0;
font-size: 1em;
color: green;
border-bottom: 1px dashed;
transition: .3s;
}
.con {
padding: 0 18px;
margin: 3px 0;
max-height: 0;
overflow: hidden;
transition: .3s ease-out;
background-color: #f1f1f1;
box-shadow: 5px 5px 10px 3px inset rgba(0,0,0,0.60);
}
Does <button class=col>this</button> work?
<div class=con>
<p>Yes!</p>
And what about <button class=col>this</button>?
<div class=con>
<p>And the transition is super fast</p>
</div>
<p>Yes but there is a delay if you close it</p>
</div>
There is something to fix.
我认为真正的解决方案在这个页面中,但我无法适应我的情况。
解决方案
无需计算动态高度。 按钮内有按钮的示例
{}
推荐阅读
- node.js - 如何从相同的秘密在 Node.js 中生成“非确定性”哈希?
- python - 我想让机器人在其中创建一个类别和一个文本或语音聊天
- java - AppCompatImageView 无法转换为 com.squareup.picasso.Target
- performance - 测试具有长时间运行查询且轮询状态的 http rest API
- web-component - “slotchange”事件不会在 iOS 11 上触发
- html - 为什么额外的垂直滚动条出现在 mat-tab-body-content 中,我需要使用 overflow-auto height 100%
- python - 如何在使用 pdfkit.from_string() 生成的 pdf 中添加换行符?
- java - 我如何模拟方法,从另一种方法调用一个方法?
- json - 需要通过 JSON Patch 在 JSON 中添加列表
- python - CSS Style 可以像 wxpython 框架那样实现 gui python 应用吗