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

              
                <button>toggle</button>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus sodales tempus accumsan. Suspendisse sit amet convallis turpis. Fusce auctor porttitor lobortis. Maecenas luctus sem vel lacinia tempor. Morbi sit amet turpis id ligula accumsan mattis. Proin lacinia, ipsum a dignissim tempor, risus dui aliquam felis, non eleifend urna nisl sed mauris. Nullam congue vestibulum venenatis.</div>
              
            
!

CSS

              
                div {
  display: none;
  overflow: hidden;
  width: 500px;
  padding: 20px;
  background: lightblue;
}
              
            
!

JS

              
                const div = document.querySelector('div');
let isActive = false;

document.querySelector('button').addEventListener('click', () => {
  if (isActive) {
    isActive = false;
    slideUp(div, 0.5);
  } else {
    isActive = true;
    slideDown(div, 0.5);
  }
});

const PADDING_TOP_ATTRIBUTE = 'data-padding-top';
const PADDING_BOTTOM_ATTRIBUTE = 'data-padding-bottom';

/**
 * Replicates jQuery's slideDown - Display the element with a sliding motion.
 * Assumes the element is hidden with display: none;
 * 
 * @params { DOMElement }
 * @params { Number } in seconds, so 0.3 is 300ms
 */
const slideDown = (element, duration = 0.3) => {
    
  element.style.display = 'block';
  element.style.height = 'auto';

  const paddingTop = getPaddingTop(element);
  element.style.paddingTop = 0;

  const paddingBottom = getPaddingBottom(element);
  element.style.paddingBottom = 0;

  const targetHeight = element.offsetHeight;
  element.style.height = 0;

  gsap.to(element, { 
    height: targetHeight, 
    paddingTop: paddingTop, 
    paddingBottom: paddingBottom,
    overwrite: 'auto', 
    duration: duration,
    ease: 'power1.out'
  } );

}

/**
 * Set the padding top for reference on the element in case a slideUp is toggled before the animation has been completed
 * 
 * @params { DOMElement }
 * 
 * @returns { Number } 
 */
const getPaddingTop = (element) => {
  if (element.getAttribute(PADDING_TOP_ATTRIBUTE)) {
    return parseFloat(element.getAttribute(PADDING_TOP_ATTRIBUTE));
  } else {
    return parseFloat(setAndGetAttribute(element, PADDING_TOP_ATTRIBUTE, parseFloat(getComputedStyle(element).paddingTop)));
  }
}

/**
 * Set the padding bottom for reference on the element in case a slideUp is toggled before the animation has been completed
 * 
 * @params { DOMElement }
 * 
 * @returns { Number } 
 */
const getPaddingBottom = (element) => {
  if (element.getAttribute(PADDING_BOTTOM_ATTRIBUTE)) {
    return parseFloat(element.getAttribute(PADDING_BOTTOM_ATTRIBUTE));
  } else {
    return parseFloat(setAndGetAttribute(element, PADDING_BOTTOM_ATTRIBUTE, parseFloat(getComputedStyle(element).paddingBottom)));
  }
}

/**
 * Set and get an attribute on an element
 * 
 * @param { DOMElement }  
 * @param { String }
 * @param { String }
 * 
 * @returns { String }  
 */
const setAndGetAttribute = (element, attribute, value) => {
  element.setAttribute(attribute, value)
  return element.getAttribute(attribute);
}

/**
 * Replicates jQuery's slideUp - Hide the element with a sliding motion.
 * 
 * @params { DOMElement }
 * @params { Number } in seconds, so 0.3 is 300ms
 */
const slideUp = (element, duration = 0.3) => {
  const paddingTop = getPaddingTop(element);  
  const paddingBottom = getPaddingBottom(element);
  gsap.to(element, {  
        height: 0, 
        paddingTop: 0, 
        paddingBottom: 0, 
        overwrite: 'auto', 
        duration: duration, 
        ease: 'power1.in',
        onComplete: () => onSlideUpComplete(element, paddingTop, paddingBottom)
    } );
}

/**
 * Revert to it's original state
 * 
 * @params { DOMElement }
 * @params { Number }
 * @params { Number }
 */
const onSlideUpComplete = (element, paddingTop, paddingBottom) => {
    element.style.display = 'none';
    element.style.height = 'auto';
    element.style.paddingTop = `${paddingTop}px`;
    element.style.paddingBottom = `${paddingBottom}px`;
    element.removeAttribute(PADDING_TOP_ATTRIBUTE);
    element.removeAttribute(PADDING_BOTTOM_ATTRIBUTE);
}
              
            
!
999px

Console