LOADING...

Preview

Pen ID
Unlock Campus Themeforest adv

 

Code

Collapsible Accordion

An accordion that opens and closes when the heading panel is clicked.

Section 1

Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.

Section 2

Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit faucibus urna.

Section 3

Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis. Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.

  • List item one
  • List item two
  • List item three

Section 4

Cras dictum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia mauris vel est.

Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

Section 1

Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam.

Section 2

Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam.

Section 3

Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam.

  • Some stuff
  • Some other things
  • And some others
CSS
* {
  padding: 0;
  margin: 0;
  font-family: 'Open Sans';
  font-size: 100%;
  line-height: 30px;
}

body {
  background-color: lightblue;
  margin: 15px;
}

h1 {
  text-align: center;
  margin-top: 20px;
  font-size: 1.6em;
}

h2 {
  font-size: 1.2em;
}

.blurb {
  text-align: center;
  padding-top: 10px;
}

.accordion {
  border-radius: 4px;
  border: 1px solid #1a1a1a;
  margin: 15px 0;
}
.accordion__header {
  cursor: pointer;
  background-color: #1a1a1a;
  color: #fff;
  border-bottom: 1px solid #000;
}
.accordion__header, .accordion__content {
  padding: 15px;
}
.accordion__header:hover {
  background-color: #4c4c4c;
  -webkit-transition: all linear 0.15s;
  transition: all linear 0.15s;
}
.accordion__header--active {
  background-color: #4c4c4c;
}
.accordion__content {
  display: none;
  border-top: 0;
  background-color: white;
}
.accordion__content li {
  margin-left: 40px;
}
JS
$(document).ready(function() {
  function accordion() {

    var $this = $(this);
    var $thisNext = $this.next();
    var $thisParent = $this.parent();

    // If accordion__content is hidden
    if ($thisNext.is(':hidden')) {
      // Go back up to parent, find the class with accordion__header--active, remove classes, then slide up the content
      $thisParent.find('.accordion__header--active')
        .removeClass('accordion__header--active')
        .next()
        .slideUp();
      // Then slide down the content of whichever header is clicked
      $thisNext.slideDown();
      // Add active class to header 
      $this.addClass('accordion__header--active');

    } else {
      // If it's not hidden, slide up the content
      $thisNext.slideUp();
      // Remove the active classes
      $this.removeClass('accordion__header--active');
    }
  };
  $('.accordion').on('click', '.accordion__header', accordion);
});

Description

Added event delegation and active states
Term
Wed, 12/27/2017 - 06:57

Related Codes

Pen ID
Pen ID
Pen ID
Square Adv