Last active
February 5, 2016 17:12
-
-
Save IvanofSA/85d44d06e4b59ae3f59f to your computer and use it in GitHub Desktop.
accordion
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
<div id="accordion"> | |
<h2>Tab1</h2> | |
<div class="tab1 active"> | |
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Hic, nulla!</p> | |
</div> | |
<h2>Tab2</h2> | |
<div class="tab2"> | |
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maxime, corrupti!</p> | |
</div> | |
<h2>Tab3</h2> | |
<div class="tab3"> | |
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Placeat, nobis.</p> | |
</div> | |
</div> |
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
.tab1, .tab2, .tab3{ | |
display: none; | |
margin: 0 auto; | |
} | |
.active{ | |
display:block; | |
} |
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
(function(){ | |
var accordion = document.getElementById('accordion'), | |
titles = accordion.getElementsByTagName('h2'); | |
for ( var i=0; i<titles.length; i++ ){ | |
titles[i].addEventListener('click', displayAccordion); | |
} | |
function displayAccordion() { | |
content = this.nextSibling.nextSibling; | |
console.log(content) | |
if ( !content.classList.contains('active') ) { | |
content.classList.add('active') | |
} else { | |
content.classList.remove('active') | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment