<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<!DOCTYPE html><html class=''>
<head><script src='//production-assets.codepen.io/assets/editor/live/console_runner-079c09a0e3b9ff743e39ee2d5637b9216b3545af0de366d4b9aad9dc87e26bfd.js'></script><script src='//production-assets.codepen.io/assets/editor/live/events_runner-73716630c22bbc8cff4bd0f07b135f00a0bdc5d14629260c3ec49e5606f98fdd.js'></script><script src='//production-assets.codepen.io/assets/editor/live/css_live_reload_init-2c0dc5167d60a5af3ee189d570b1835129687ea2a61bee3513dee3a50c115a77.js'></script><meta charset='UTF-8'><meta name="robots" content="noindex"><link rel="shortcut icon" type="image/x-icon" href="//production-assets.codepen.io/assets/favicon/favicon-8ea04875e70c4b0bb41da869e81236e54394d63638a1ef12fa558a4a835f1164.ico" /><link rel="mask-icon" type="" href="//production-assets.codepen.io/assets/favicon/logo-pin-f2d2b6d2c61838f7e76325261b7195c27224080bc099486ddd6dccb469b8e8e6.svg" color="#111" /><link rel="canonical" href="https://codepen.io/carlosdelreal/pen/XXQZVQ?limit=all&page=28&q=Accordion" />
<style class="cp-pen-styles">body {
overflow-y: scroll;
}
.accordion-panel {
font-family: Helvetica, Arial, sans-serif;
margin: 50px auto;
max-width: 560px;
}
.accordion-panel .accordion {
padding-top: 30px;
}
.accordion-panel .accordion dt {
display: block;
padding: 25px;
background: #E3E1E2;
color: #555;
cursor: pointer;
position: relative;
user-select: none;
}
.accordion-panel .accordion dd {
height: 0;
overflow: hidden;
transition: height .35s ease-out;
margin-left: 0;
margin-bottom: 20px;
background: #F4F4F4;
}
.accordion-panel .accordion .content {
padding: 25px;
overflow: auto;
}
.accordion-panel .plus-icon {
display: inline-block;
width: 15px;
height: 15px;
position: relative;
transition: transform 0.35s ease-out;
position: absolute;
right: 35px;
top: 50%;
margin-top: -12px;
}
.accordion-panel .plus-icon:before,
.accordion-panel .plus-icon:after {
content: '';
background: #6bbf49;
position: absolute;
}
.accordion-panel .plus-icon:before {
width: 2px;
height: 15px;
margin-left: -0.5px;
left: 50%;
}
.accordion-panel .plus-icon:after {
width: 15px;
height: 2px;
margin-top: -0.5px;
top: 50%;
opacity: 1;
transition: opacity 0.35s ease-out;
}
.accordion-panel .is-open .plus-icon {
transform: rotate(90deg);
}
.accordion-panel .is-open .plus-icon:after {
opacity: 0;
}
.accordion-panel .hidden {
display: none;
}
.accordion-panel .open-btn,
.accordion-panel .close-btn {
cursor: pointer;
position: absolute;
right: 0;
width: 90px;
}
.accordion-panel .buttons-wrapper {
position: relative;
}
.accordion-panel .buttons-wrapper .plus-icon {
position: absolute;
right: 10px;
top: 12px;
}
</style></head><body>
<div class="accordion-panel">
<div class="buttons-wrapper">
<i class="plus-icon"></i>
<div class="open-btn">
Open all
</div>
<div class="close-btn hidden">
Close all
</div>
</div>
<dl class="accordion">
<dt>item 1 <i class="plus-icon"></i></dt>
<dd>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </p>
</div>
</dd>
<dt>item 2 <i class="plus-icon"></i></dt>
<dd>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</dd>
<dt>item 3 <i class="plus-icon"></i></dt>
<dd>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</dd>
</dl>
</div>
<script src='//production-assets.codepen.io/assets/common/stopExecutionOnTimeout-b2a7b3fe212eaa732349046d8416e00a9dec26eb7fd347590fbced3ab38af52e.js'></script><script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script>
<script >$(document).ready(function() {
var bodyEl = $('body'),
accordionDT = $('.accordion').find('dt'),
accordionDD = accordionDT.next('dd'),
parentHeight = accordionDD.height(),
childHeight = accordionDD.children('.content').outerHeight(true),
newHeight = parentHeight > 0 ? 0 : childHeight,
accordionPanel = $('.accordion-panel'),
buttonsWrapper = accordionPanel.find('.buttons-wrapper'),
openBtn = accordionPanel.find('.open-btn'),
closeBtn = accordionPanel.find('.close-btn');
bodyEl.on('click', function(argument) {
var totalItems = $('.accordion').children('dt').length;
var totalItemsOpen = $('.accordion').children('dt.is-open').length;
if (totalItems == totalItemsOpen) {
openBtn.addClass('hidden');
closeBtn.removeClass('hidden');
buttonsWrapper.addClass('is-open');
} else {
openBtn.removeClass('hidden');
closeBtn.addClass('hidden');
buttonsWrapper.removeClass('is-open');
}
});
function openAll() {
openBtn.on('click', function(argument) {
accordionDD.each(function(argument) {
var eachNewHeight = $(this).children('.content').outerHeight(true);
$(this).css({
height: eachNewHeight
});
});
accordionDT.addClass('is-open');
});
}
function closeAll() {
closeBtn.on('click', function(argument) {
accordionDD.css({
height: 0
});
accordionDT.removeClass('is-open');
});
}
function openCloseItem() {
accordionDT.on('click', function() {
var el = $(this),
target = el.next('dd'),
parentHeight = target.height(),
childHeight = target.children('.content').outerHeight(true),
newHeight = parentHeight > 0 ? 0 : childHeight;
// animate to new height
target.css({
height: newHeight
});
// remove existing classes & add class to clicked target
if (!el.hasClass('is-open')) {
el.addClass('is-open');
}
// if we are on clicked target then remove the class
else {
el.removeClass('is-open');
}
});
}
openAll();
closeAll();
openCloseItem();
});
//# sourceURL=pen.js
</script>
</body></html>