cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
               <section class="card">
  <article>
    <h1 data-accordion-element-trigger>Lorem ipsum</h1>
    <div data-accordion-element-content class="content">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse eu lectus quis mauris malesuada fringilla. Praesent non tellus massa. Phasellus at neque et massa elementum sodales. Quisque neque enim, dignissim at molestie quis, lobortis eu elit. Sed ornare ultrices tortor sed mollis. Pellentesque blandit quam imperdiet lorem lobortis, at pharetra massa condimentum. Maecenas ac nibh mi. Donec suscipit dolor vitae faucibus venenatis.</p>
    </div>
  </article>
  <article>
    <h1 data-accordion-element-trigger>Lorem ipsum</h1>
    <div data-accordion-element-content class="content">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse eu lectus quis mauris malesuada fringilla. Praesent non tellus massa. Phasellus at neque et massa elementum sodales. Quisque neque enim, dignissim at molestie quis, lobortis eu elit. </p>
    </div>
  </article>
  <article>
    <h1 data-accordion-element-trigger>Lorem ipsum</h1>
    <div data-accordion-element-content class="content">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse eu lectus quis mauris malesuada fringilla. Praesent non tellus massa. Phasellus at neque et massa elementum sodales. Quisque neque enim, dignissim at molestie quis, lobortis eu elit. Sed ornare ultrices tortor sed mollis. Pellentesque blandit quam imperdiet lorem lobortis, at pharetra massa condimentum. Maecenas ac nibh mi. Donec suscipit dolor vitae faucibus venenatis.</p>
    </div>
  </article>
  <article>
    <h1 data-accordion-element-trigger>Lorem ipsum</h1>
    <div data-accordion-element-content class="content">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse eu lectus quis mauris malesuada fringilla. Praesent non tellus massa. Phasellus at neque et massa elementum sodales. Quisque neque enim, dignissim at molestie quis, lobortis eu elit. Sed ornare ultrices tortor sed mollis. Pellentesque blandit quam imperdiet lorem lobortis, at pharetra massa condimentum. Maecenas ac nibh mi. Donec suscipit dolor vitae faucibus venenatis.<br />Praesent non tellus massa. Phasellus at neque et massa elementum sodales. Quisque neque enim, dignissim at molestie quis, lobortis eu elit. Sed ornare ultrices tortor sed mollis. Pellentesque blandit quam imperdiet lorem lobortis, at pharetra massa condimentum. Maecenas ac nibh mi. Donec suscipit dolor vitae faucibus venenatis.</p>
    </div>
  </article>
</section>
            
          
!
            
              // NECESSARY
 
[data-accordion-element-content] {
  height: auto;
  overflow: hidden;
  transition: height .5s;
}

// JUST TO STYLE THIS PEN

* {
  box-sizing: border-box;
}

body {
    background: #eee;
    overflow: scroll;
}

.card {
  width: 400px;
  margin: 50px auto;
  border-radius: 2px;
  background: white;
  box-shadow: 0 2px 2px 0 rgba(0,0,0,0.14), 0 1px 5px 0 rgba(0,0,0,0.12), 0 3px 1px -2px rgba(0,0,0,0.2);
}
  
article {
  position: relative;
  font-family: 'Roboto', sans-serif;
  transition: all .3s;
  background: #E8EAF6;
  
  &.selected {
    transform: scale(1.1);
    box-shadow: 0 0 5px rgba(2, 2, 2, 0.4);
    z-index: 2;
  } 
}
  
h1 {
  position: relative;
  background: #3F51B5;
  padding: 20px 30px;
  font-size: 20px;
  line-height: 30px;
  color: #fff;
  overflow: hidden;
  transition: background .3s;
  
  &:hover,
  .selected & {
    cursor: pointer;
    background: #3949AB;
  }

  &:before,
  &:after {
    content: "";
    position: absolute;
    right: 0;
    top: 50%;
    margin-top: -1px;
    margin-right: 30px;
    width: 20px;
    height: 2px;
    background: #fff;
    transition: transform .3s;
  }

  &:before {
    transform: rotate(0deg);
    animation: close-line-one-in .8s forwards;
    
    .selected & {
      animation: close-line-one-out .8s forwards;
    }
  }
  
  &:after {
    transform: rotate(90deg);
    animation: close-line-two-in .8s forwards;
    
    .selected & {
      animation: close-line-two-out .8s forwards;  
    }
  }

}

.content {  
  opacity: 0;
  transform: translateY(20px);
  transition: all ease-out .4s .1s;
  
  .selected & {
    transform: translateY(0);
    opacity: 1;
  }
}

p {
  padding: 25px 30px 40px;
  background: #E8EAF6;
  font-size: 16px;
  line-height: 24px;
  color: #000;
}


@keyframes close-line-one-out {
    0%, 50% {
      transform: translateX(0);
    }
    75% { 
      transform: translateX(50px);
    }
    76% { 
      transform: translateX(50px) translateY(50px);
    }
    100% { 
      transform: rotate(45deg);
    }
}

@keyframes close-line-one-in {
    0%, 20% {
      transform: rotate(45deg);
    }
    60% { 
      transform: translateX(50px) translateY(50px) rotate(45deg);
    }
    61% { 
      transform: translateX(50px);
    }
    100% { 
      transform: rotate(0deg);
    }
}

@keyframes close-line-two-out {
    0%, 50% { 
      transform: rotate(90deg);
    }
    75% { 
      transform: translateY(-50px) rotate(90deg);
    }
    76% {
      transform: translateY(-50px) translateX(50px) rotate(90deg);
    }
    100% { 
      transform: rotate(135deg);
    }
}

@keyframes close-line-two-in {
    0% { 
      transform: rotate(135deg);
    }
    50% { 
      transform: translateY(-50px) translateX(50px) rotate(135deg);
    }
    51% { 
      transform: translateY(-50px) rotate(90deg);
    }
    100% { 
      transform: rotate(90deg);
    }
}
            
          
!
            
              /**
>>>>>>>

Wanna include in your project?

More documentation on github:

https://github.com/cant89/gianni-accordion-js

>>>>>>>
**/

var gianniAccordion = (function(){
  return class {      
    
    transitionendEventName(){
      var i,
          el = document.createElement('div'),
          transitions = {
            'transition': 'transitionend',
            'OTransition': 'otransitionend',
            'MozTransition': 'transitionend',
            'WebkitTransition': 'webkitTransitionEnd'
          };

      for (i in transitions) {
        if (transitions.hasOwnProperty(i) && el.style[i] !== undefined) {
          return transitions[i];
        }
      }
    }

    expand(el){
      function resetHeight(ev){
        if(ev.target != el.content) return
        el.content.removeEventListener(this.transitionendevent, bindEvent);

        if(!el.isOpen) return

        requestAnimationFrame(()=>{      
          el.content.style.transition = '0';
          el.content.style.height = 'auto';

          requestAnimationFrame(()=>{
            el.content.style.height = null;
            el.content.style.transition = null;

            this.fire("elementOpened", el);
          });
        });
      }

      var bindEvent = resetHeight.bind(this);
      el.content.addEventListener(this.transitionendevent, bindEvent);

      el.isOpen = true;
      el.content.style.height = el.content.scrollHeight + "px";
    }

    collapse(el){

      function endTransition(ev){     
        if(ev.target != el.content) return
        el.content.removeEventListener(this.transitionendevent, bindEvent);

        if(el.isOpen) return

        this.fire("elementClosed", el);
      }

      var bindEvent = endTransition.bind(this);
      el.content.addEventListener(this.transitionendevent, bindEvent);

      el.isOpen = false;    

      requestAnimationFrame(()=>{
        el.content.style.transition = '0';
        el.content.style.height = el.content.scrollHeight + "px";

        requestAnimationFrame(()=>{
          el.content.style.transition = null;
          el.content.style.height = 0;
        });
      });
    }

    open(el){
      el.selected = true;
      this.fire("elementSelected", el);
      this.expand(el);
      el.wrapper.classList.add(this.selectedClass);
    }

    close(el){
      el.selected = false;
      this.fire("elementUnselected", el);
      this.collapse(el);
      el.wrapper.classList.remove(this.selectedClass);
    }

    toggle(el){
      if(el.selected){
        this.close(el);
      } else {
        this.open(el);

        if(this.oneAtATime){        
          this.els.filter(e=>e!=el && e.selected).forEach(e=>{
            this.close(e);
          });
        }
      }
    }

    attachEvents(){    
      this.els.forEach((el, i)=>{
        el.trigger.addEventListener("click", this.toggle.bind(this, el));
      });
    }

    setDefaultData(){    
      this.els = [];
      this.events = {
        'elementSelected': [],
        'elementOpened': [],
        'elementUnselected': [],
        'elementClosed': []
      };
      this.transitionendevent = this.transitionendEventName();
      this.oneAtATime = true;
      this.selectedClass = "selected";
      this.trigger = "[data-accordion-element-trigger]";
      this.content = "[data-accordion-element-content]";
    }

    setCustomData(data){
      var keys = Object.keys(data);

      for(var i=0; i<keys.length; i++){
        this[ keys[i] ] = data[ keys[i] ];
      }
    }

    fire(eventName, el){
      var callbacks = this.events[eventName];
      for(var i=0; i<callbacks.length; i++){
        callbacks[i]( el )
      }
    }

    on(eventName, cb){
      if( !this.events[eventName] ) return       
      this.events[eventName].push(cb);
    }

    constructor(data){    
      this.setDefaultData();
      this.setCustomData(data); // ES6 => Object.assign(this, data)

      [].forEach.call( document.querySelectorAll(this.elements), (el, i)=>{
        this.els.push({
          wrapper: el,
          trigger: el.querySelector(this.trigger),
          content: el.querySelector(this.content)
        });

        this.els[i].content.style.height = 0;
      });

      this.attachEvents();
    }
    
  }
})();

var myAccordion = new gianniAccordion({
  elements: ".card article"
});

myAccordion.on("elementSelected", (data)=>{
  console.log("elementOpened", data);
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console