Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <ul class="fancybox">
  <li data-jsfancytitle="Some Title">
    This is some content This is some content This is some content This is some content This is some content This is some content This is some content This is some content
  </li>
  <li data-title="Another title">This is some more content wooo</li>
  <li>Praise be to his glorious name!</li>
</ul>
<hr>
<ul class="fancybox">
  <li data-jsfancytitle="More Title">
    This is some more content This is some more content This is some more content This is some more content This is some more content
  </li>
  <li data-title="Another title">This is some more more content wooo</li>
  <li>More praise be to his glorious name!</li>
</ul>
              
            
!

CSS

              
                .js-ftoggle {
  padding: 0px;
  max-width: 500px;
}

.js-ftoggle .js-fchild {
  display: block;
  margin-bottom: 15px;
  border-bottom: solid 1px #ddd;
  cursor: pointer;
  overflow: hidden;
  height: auto;
  -webkit-transform: translate3d(0, 0, 0);
  -moz-transform: translate3d(0, 0, 0);
  transform: translate3d(0, 0, 0);
}

.js-ftoggle .title {
  padding: 15px;
  background: #efefef;
}

.js-ftoggle .content {
  overflow: hidden;
  padding: 15px;
  background: #ccc;
}

.js-ftoggle .content-inner {
  background: #eee
}
              
            
!

JS

              
                $.fn.fancytoggle = function(options) {

  options = $.extend({
    openSpeed: 400,
    closeSpeed: 400,
    easing: 'linear',
    singleItemOpen: false,
    onItemClick: $.noop,
    onItemOpen: $.noop,
    onItemClosed: $.noop,
    initialOpenElement: ''
  }, options);

  this.each(function() {

    $(this).addClass('js-ftoggle');
    var $children = $(this).children();

    $(window).on('resize', function() {
      setItemMinMaxHeights($children);
    });

    $children.each(function() {
      var $item = $(this);
      $item.addClass('js-fchild');
      $item.wrapInner('<div class="content"></div>');

      var title = $item.attr('data-jsfancytitle') || 'Toggle';
      $item.prepend('<div class="title">' + title + '</div>');

      //on title click, togle it 
      $item.on('click', '.title', function(event) {
        toggle($(this).parent(), event);
      });

    });
    setItemMinMaxHeights($children);

    //if we have an element set to be opened on load
    if (options.initialOpenElement) {
      var element = $(this).children(options.initialOpenElement);
      animate(element, 'data-max-height', 'closeSpeed');
    }

  });

  //animate toggle close / open
  function animate($item, height, dirSpeed) {
    $item.css('height', $item.attr(height) + 'px');
    $item.css({
      'transition': 'height ' + options.easing + ' ' + $item.attr(dirSpeed) + 'ms'
    }).toggleClass('active');
  }

  //toggle when clicked
  function toggle($item, event) {

    options.onItemClick.call(null, $item, event);

    if (options.singleItemOpen === true) {
      var $activePanel = $item.siblings('.active');
      animate($activePanel, 'data-min-height', 'data-close-speed');
    }

    if ($item.hasClass('active')) {
      animate($item, 'data-min-height', 'data-close-speed');
      options.onItemClose.call(null, $item, event);
    } else {
      animate($item, 'data-max-height', 'data-open-speed');
      options.onItemOpen.call(null, $item, event);
    }
  }

  //calculate min /max heights and apply them to toggle children 
  function setItemMinMaxHeights($children) {
    $children.each(function() {
      var $item = $(this);
      var $title = $item.children('.title');
      var $content = $item.children('.content');
      var maxHeight = 0;
      var minHeight = 0;

      if ($title) {
        maxHeight += $title.outerHeight(true);
        minHeight += $title.outerHeight(true);
      }
      if ($content) {
        maxHeight += $content.outerHeight(true);
      }

      $item.attr('data-max-height', maxHeight);
      $item.attr('data-min-height', minHeight);
      $item.attr('data-close-speed', options.closeSpeed);
      $item.attr('data-open-speed', options.openSpeed)

      if ($item.hasClass('active')) {
        $item.height(maxHeight);
      } else {
        $item.height(minHeight);
      }
    });
  }

  return this;
}

$('.fancybox').fancytoggle({
  openSpeed: 400,
  closeSpeed: 600,
  easing: 'linear',
  singleItemOpen: true,
  onItemOpen: function($item, event) {
    console.log("Opened");
  },
  onItemClose: function($item, event) {
    console.log("Closed");
  },
  onItemClick: function($item, event) {
    console.log("Clicked");
  },
  initialOpenElement: 'li:last-child'
});
              
            
!
999px

Console