Last active
December 16, 2015 07:39
-
-
Save drainpip/5400445 to your computer and use it in GitHub Desktop.
Super simple accordion. Assumes jQuery. Style it for bad ass options. The script as written will close anything that's open upon clicking any .accordion-button object. Adding an extra if statement to check will stop that if you wish, but then it's technically not an accordion.
This file contains hidden or 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
| <style> | |
| .accordion-button {cursor:pointer;} /* visual cue for user */ | |
| .accordion-button.on { /* insert style for on state */ } | |
| .accordion-content {display:none; overflow:hidden;} /* Hide on page load, overflow:hidden is to keep things from hopping around */ | |
| .accordion-content.open {display:block;} /* allows to set one open on page load | |
| </style> | |
| <ul> | |
| <li class="accordion-button">Title / heading here</li> | |
| <li class="accordion-content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam in nibh eget augue tincidunt elementum nec vel tellus. Fusce vestibulum dictum leo, ac varius nisi sagittis vel. Aenean sed est id arcu adipiscing semper eu vel mi. In bibendum est sit amet urna gravida vel hendrerit sapien pharetra.</li> | |
| <li class="accordion-button">Title / heading here</li> | |
| <li class="accordion-content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam in nibh eget augue tincidunt elementum nec vel tellus. Fusce vestibulum dictum leo, ac varius nisi sagittis vel. Aenean sed est id arcu adipiscing semper eu vel mi. In bibendum est sit amet urna gravida vel hendrerit sapien pharetra.</li> | |
| </ul> | |
| // jQuery is required | |
| $(document).ready(function() { | |
| $('.accordion-button').click(function() { | |
| $(this).parent().children('.accordion-button').removeClass('on'); | |
| $(this).parent().children('.accordion-content').slideUp('normal', function() { | |
| $(this).removeClass('open'); | |
| }); | |
| if($(this).next().is(':hidden') == true) { | |
| $(this).addClass('on'); | |
| $(this).next().slideDown('normal').addClass('open'); | |
| } | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment