Beispiel: ohne großes Framework
HTML:
<!DOCTYPE html >
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Accordion</title>
<link rel="stylesheet" type="text/css" href="accordion.css" />
</head>
<body>
<ul class="accordion">
<li class="active">
<h4 class="sheet-header">Udo</h4>
<div class="imageLeft"><img border="0" height="115" width="100" src="/media/udo.jpg" alt="Udo Schmal" title="Udo Schmal" /></div>
<h5>Udo Schmal</h5>
<p><strong>Softwareentwicklung</strong></p>
<table summary="Kontakt" class="noBorder">
<tbody>
<tr>
<td>E-Mail:</td>
<td><a href="mailto:udo.schmal@t-online.de">udo.schmal@t-online.de</a></td>
</tr>
</tbody>
</table>
</li>
<li>
<h4 class="sheet-header">Claudia</h4>
<div class="imageLeft"><img border="0" height="115" width="100" src="/media/claudia.jpg" alt="Claudia Schmal-Prangs" title="Claudia Schmal-Prangs" /></div>
<h5>Claudia Schmal-Prangs</h5>
<table summary="Kontakt" class="noBorder">
<tbody>
<tr>
<td>E-Mail:</td>
<td><a href="mailto:claudia.prangs@t-online.de">claudia.prangs@t-online.de</a></td>
</tr>
</tbody>
</table>
</li>
</ul>
<script src="accordion.js"></script>
</body>
</html>
JavaScript:
// coding: utf-8
/** Created by: Udo Schmal | https://www.gocher.me/ */
(function () {
'use strict';
function Accordion(accordion) {
var multiselect = accordion.getAttribute('data-multiselect') || false;
var current = null;
var sheets = accordion.children;
function toggle(event) {
if (multiselect) {
this.classList.toggle("active");
} else {
if (current != this) {
if (current) {
current.classList.remove("active");
}
current = this;
this.classList.add("active");
}
}
event.preventDefault();
}
for (var i=0; i<sheets.length; i++) {
if (!multiselect && sheets[i].classList.contains("active")) {
current = sheets[i];
} else if (!multiselect && i==0) {
current = sheets[i];
}
sheets[i].addEventListener('mousedown', toggle);
}
current.classList.add("active");
}
var accordionList = document.querySelectorAll("ul.accordion");
for (var i=0; i<accordionList.length; i++) {
new Accordion(accordionList[i]);
}
})();
StyleSheet:
ul.accordion {
font-family: Arial,sans-serif;
position: relative;
margin: 0;
padding: 0;
list-style: none;
list-style-type: none;
}
ul.accordion .sheet-header {
margin-top: 0;
}
ul.accordion > li {
margin: 0 0 1px 0;
padding: 7px 10px 1px 10px;
border: 1px solid #C8CED0;
background-color: #fff;
border-radius: 4px;
max-height: 24px;
overflow: hidden;
transition: max-height 0.25s ease-out;
}
ul.accordion > li:not(.active):hover {
cursor: pointer;
}
ul.accordion > li.active {
height: auto;
max-height: 1200px;
padding-bottom: 7px;
box-sizing: border-box;
transition: max-height 0.25s ease-in;
overflow: hidden;
}
ul.accordion > li .sheet-header {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
color: #666;
font-weight: bold;
}