A Pen by Christoph Niedermoser on CodePen.
Last active
September 4, 2019 08:34
-
-
Save nimoatwoodway/47be8e92219d46efdc3b28ad87469080 to your computer and use it in GitHub Desktop.
A simple jQuery Accordion with unlimited nesting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul class="accordion"> | |
<li> | |
<a class="toggle" href="javascript:void(0);">Alarm</a> | |
<ul class="inner"> | |
<li> | |
<a href="#" class="toggle">Recipe 1</a> | |
<div class="inner"> | |
<ul> | |
<li>Step 1</li> | |
<li>Step 2</li> | |
</ul> | |
</div> | |
</li> | |
<li> | |
<a href="#" class="toggle">Recipe 2</a> | |
<div class="inner"> | |
<ul> | |
<li>Step 1</li> | |
<li>Step 2</li> | |
</ul> | |
</div> | |
</li> | |
</ul> | |
</li> | |
<li> | |
<a class="toggle" href="javascript:void(0);">Production</a> | |
<ul class="inner"> | |
<li> | |
<a href="#" class="toggle">Recipe 1</a> | |
<div class="inner"> | |
<ul> | |
<li>Step 1</li> | |
<li>Step 2</li> | |
</ul> | |
</div> | |
</li> | |
<li> | |
<a href="#" class="toggle">Recipe 2</a> | |
<div class="inner"> | |
<ul> | |
<li>Step 1</li> | |
<li>Step 2</li> | |
</ul> | |
</div> | |
</li> | |
</ul> | |
</li> | |
<li> | |
<a class="toggle" href="javascript:void(0);">Helper</a> | |
<ul class="inner"> | |
<li> | |
<a href="#" class="toggle">Recipe 1</a> | |
<div class="inner"> | |
<ul> | |
<li>Step 1</li> | |
<li>Step 2</li> | |
</ul> | |
</div> | |
</li> | |
<li> | |
<a href="#" class="toggle">Recipe 2</a> | |
<div class="inner"> | |
<ul> | |
<li>Step 1</li> | |
<li>Step 2</li> | |
</ul> | |
</div> | |
</li> | |
</ul> | |
</li> | |
<li> | |
<ul class="inner"> | |
<li> | |
<a href="#" class="toggle">Technically any number of nested elements</a> | |
<ul class="inner"> | |
<li> | |
<a href="#" class="toggle">Another nested element</a> | |
<div class="inner"> | |
<p> | |
As long as the inner element has inner as one of its classes then it will be toggled. | |
</p> | |
<p> | |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas tempus placerat fringilla. Duis a elit et dolor laoreet volutpat. Aliquam ultrices mauris id mattis imperdiet. Aenean cursus ultrices justo et varius. Suspendisse aliquam orci id dui dapibus | |
blandit. In hac habitasse platea dictumst. Sed risus velit, pellentesque eu enim ac, ultricies pretium felis. | |
</p> | |
</div> | |
</li> | |
</ul> | |
</li> | |
<li>Option 2</li> | |
<li>Option 3</li> | |
</ul> | |
</li> | |
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('.toggle').click(function(e) { | |
e.preventDefault(); | |
var $this = $(this); | |
if ($this.next().hasClass('show')) { | |
$this.next().removeClass('show'); | |
$this.next().slideUp(350); | |
} else { | |
$this.parent().parent().find('li .inner').removeClass('show'); | |
$this.parent().parent().find('li .inner').slideUp(350); | |
$this.next().toggleClass('show'); | |
$this.next().slideToggle(350); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import url('https://fonts.googleapis.com/css?family=Pacifico|Open+Sans:300,400,600'); | |
* { | |
box-sizing: border-box; | |
font-family: 'Open Sans',sans-serif; | |
font-weight: 300; | |
} | |
a { | |
text-decoration: none; | |
color: inherit; | |
} | |
p { | |
font-size:1.1em; | |
margin: 1em 0; | |
} | |
.description { | |
margin: 1em auto 2.25em; | |
} | |
body { | |
width: 40%; | |
min-width: 300px; | |
max-width: 400px; | |
margin: 1.5em auto; | |
color: #333; | |
} | |
h1 { | |
font-family: 'Pacifico', cursive; | |
font-weight: 400; | |
font-size: 2.5em; | |
} | |
ul { | |
list-style: none; | |
padding: 0; | |
.inner { | |
padding-left: 1em; | |
overflow: hidden; | |
display: none; | |
&.show { | |
/*display: block;*/ | |
} | |
} | |
li { | |
margin: .5em 0; | |
a.toggle { | |
width: 100%; | |
display: block; | |
background: rgba(0,0,0,0.78); | |
color: #fefefe; | |
padding: .75em; | |
border-radius: 0.15em; | |
transition: background .3s ease; | |
&:hover { | |
background: rgba(0, 0, 0, 0.9); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment