折叠面板源码
时间: 2023-07-05 16:20:04 浏览: 76
jQuery实现的三维折叠面板展开收缩特效源码.zip
折叠面板(Accordion)是一种常见的Web组件,用于展示可折叠的内容区域,通常用于网站的FAQ、帮助中心等场景。以下是一个简单的HTML、CSS和JavaScript代码示例,实现了一个基本的折叠面板功能:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Accordion Demo</title>
<style>
.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
}
.active, .accordion:hover {
background-color: #ccc;
}
.panel {
padding: 0 18px;
background-color: white;
display: none;
overflow: hidden;
}
</style>
</head>
<body>
<button class="accordion">Section 1</button>
<div class="panel">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec euismod mauris vel lacinia volutpat. Nullam et ipsum et velit malesuada molestie.</p>
</div>
<button class="accordion">Section 2</button>
<div class="panel">
<p>Phasellus eget enim vel risus facilisis bibendum. Morbi dapibus nunc eu tortor mattis, vitae volutpat sapien faucibus.</p>
</div>
<button class="accordion">Section 3</button>
<div class="panel">
<p>Aliquam erat volutpat. Nam vel sapien vitae tellus eleifend hendrerit vel et magna.</p>
</div>
<script>
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.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
});
}
</script>
</body>
</html>
```
这段代码实现了一个由三个折叠面板组成的页面。点击每个折叠面板的按钮,可以展开或折叠对应的内容区域。CSS样式定义了折叠面板和内容区域的外观,JavaScript代码实现了折叠面板的交互逻辑。
阅读全文