javascript - 浮动下拉元素相互向右推
问题描述
我正在开发一个项目,其中一页上有多个下拉菜单。为了让这些下拉菜单按顺序使用,我使用左浮动,问题是当我打开左边的下拉菜单之一时,它会将其中一些推到右边。需要发生的是打开下拉菜单下方的下拉菜单需要向下移动,而不是移动到另一侧。
我曾尝试使用“clear: both”和“Overflow: auto”,但这些都不起作用。
这是一个 JSFiddle:https ://jsfiddle.net/p85ga09f/
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
});
}
* {
font-family: "Open Sans", "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif;
}
.accordion_holder {
border-bottom: 1px dashed #eaeaea;
width: 48%;
float: left;
margin: 0 1%;
}
.accordion {
background-color: #fff;
color: #444;
cursor: pointer;
padding: 18px;
padding-bottom: 30px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 1.125rem;
font-weight: 200;
transition: 0.4s;
}
.panel {
padding: 0 18px;
background-color: white;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
}
li {
line-height: 28px;
}
i {
margin-right: 10px;
}
li,
a {
color: #FF4212;
text-decoration: none;
}
ul {
margin: 0;
margin-bottom: 5px;
}
<div class="content">
<div class="accordion_holder">
<button class="accordion">
Placeholder 1
</button>
<div class="panel">
<ul>
<a href="#" target="_parent">
<li>test</li>
</a>
<a href="#" target="_parent">
<li>tset</li>
</a>
</ul>
</div>
</div>
<div class="accordion_holder">
<button class="accordion">
Placeholder 2
</button>
<div class="panel">
<ul>
<li>
test
</li>
<li>
test
</li>
<li>
test
</li>
</ul>
</div>
</div>
<div class="accordion_holder">
<button class="accordion">
Placeholder 3
</button>
<div class="panel">
<ul>
<li>
test
</li>
<li>
test
</li>
</ul>
</div>
</div>
</div>
下拉菜单需要保留在其行和列上。
任何帮助,将不胜感激。
解决方案
在每行的左侧手风琴按钮上使用“clear:left”应该可以解决您的问题。因此,在我的情况下,使用“clear:left”向占位符 3 添加一个类。
编辑:正如其他人所建议的那样;这是一个快速修复,如果您只有几个按钮,它应该可以工作。对于较大的手风琴,我建议改用 Flexbox 或 Grid。
推荐阅读
- safari - 在审查您的扩展程序期间,我们发现 Info.plist 在提交到 Safari 扩展程序库时缺少“网站”字段
- mysql - MySql - 如何组合两个表的 where 子句以进行一对多连接?
- node.js - 无法通过节点应用程序访问 docker 容器内的 url,但 curl 到相同的 url 有效
- c# - 如何在对标题应用排序后刷新 WPF Treeview 内容?
- puppeteer - 从 puppeteer 脚本中的 csv 读取数据并将数据传递到某些网站以自动填写表格,还可以通过 puppeteer 在其他网站上上传图像
- html - Flex 粘性页脚在窗口调整大小时与 flex 兄弟重叠(高度缩小)
- branch-prediction - 为什么不能在另一个过程中训练分支预测器。
- react-native - 如何在本机反应中永久获取移动设备(iOS和android)的uuid
- certificate - 具有多个帐户的 Twilio Push 凭据
- javascript - 没有为 emulateTransitionEnd 触发事件