Monday, April 2, 2018

How to Build an Accordion Component With CSS and a Touch of JavaScript

How to Build an Accordion Component With CSS and a Touch of JavaScript

In today’s tutorial we’ll learn how to build a show/hide component with CSS and a little bit of JavaScript. By default, jQuery provides the slideToggle method which allows us to create accordions with a sliding motion. Our challenge is to mimic this functionality with pure JavaScript.

Here’s the component we’ll be creating:

1. The HTML

To begin with, we define an element with the class of container which contains two sub-elements:

  • the button which will hide and reveal the content (including an inline SVG icon)
  • the actual content

Here’s what that looks like:

2. The CSS

The CSS is pretty straightforward. We need to define two helper classes (i.e. hide and show). 

To hide and reveal the target element, we’ll use the height property, but we won’t specify its values in CSS. Instead, we’ll dynamically set the height values through JavaScript. 

One thing to note is that we don’t use the display property for toggling the visibility of our content. That property doesn’t count itself among the animatable CSS properties.

Here are the corresponding CSS styles:

3. The JavaScript

Now it’s time to discuss how our JavaScript code should work.

First, as soon as the DOM is ready, we get the height of the .info element and then immediately set its value to 0.

Keep in mind that depending on your content (e.g. if it includes images) you might have to enclose the code above inside the load event.

Next, when the .toggle-btn button is clicked we do the following:

  • Toggle the visibility of the .less and .more elements.
  • Recalculate the height of the .info element. If it’s 0 (initially we give it this value), that means the content is hidden, so we reveal it by setting its height equal to its initial height (stored in the infoHeight variable). On the other hand if the content is visible, we hide it by setting its height to 0.
  • Optionally, we ensure that the .toggle-btn will be clicked only once until the slide animation finishes (it lasts 500 ms). 

Here’s the code that implements all that behavior:

Browser Resize

The next step is to ensure that the component will work correctly as the browser window gets resized. 

Here’s the necessary JS code:

4. Browser Support

Our demo should work well in all recent browsers and devices. Also, as usual, we use Babel to compile the ES6 code down to ES5. 

Conclusion

In this short tutorial we built an accordion-style show/hide component with CSS and JavaScript. Thanks to the animatable height property, we managed to add a slide-in/slide-out effect to our component. 

Notably, we haven’t considered accessibility, so if you want to enhance its functionality that certainly could be the next step.

More Tutorials “With a Touch of JavaScript”


  • JavaScript
    How to Build a Responsive Tab Component With CSS and a Touch of JavaScript
    George Martsoukos
  • Navigation Design
    How to Build an Off-Canvas Menu With CSS and a Touch of JavaScript
    George Martsoukos
  • JavaScript
    A Simple JavaScript Technique for Filling Star Ratings
    George Martsoukos
  • Navigation Design
    How to Build a Shifting Underline Hover Effect With CSS and JavaScript
    George Martsoukos

No comments:

Post a Comment