Building a Slider with Metafizzy’s Flickity

Flickity is a JavaScript slider library, built by David DeSandro of Metafizzy fame. It’s optimized for touch gestures, performance, and includes things like physics-based animation. In this tutorial we’ll get it up and running, then examine some extra features it brings to the table. Let’s go!

The Task

Let’s imagine that we’ve been asked, by a fictitious online learning company, to implement a slider on their website. Currently, they only feature three popular courses on the homepage, but they want to add more to the list and have decided that a slider sounds like a great solution.

Current State only 3 featured courses shown

The fictitious popular courses

Step 1: Get Set

Setting up Flickity is straightforward. Download flickity.pkgd.min.js as well as flickity.min.css, save these files in appropriate folders in your project directory, and link both of them within the document’s <head> tag. I prefer, however, to link them through the CDN for faster performance, as follows.

Step 2: Go!

Flickity can be initialized in three ways. First, we can use jQuery. We’d link to jQuery, then, within <script> tags run:

This could be an appropriate way when we use Flickity in conjunction with other libraries; a framework like Bootstrap, or a platform like WordPress, which already heavily depend on jQuery. This would make the code more consistent and uniform. 

If the website is fully customized, I’d recommend we go completely vanilla. So, again, within <script> tags at the bottom of the page (or in a separate, linked JS file):

Another way is by including js-flickity within the class attribute of the wrapping element. This is a slender method which I find preferable, especially when we plan to have multiple sliders, each with different options.

Having followed those setup steps, whichever method you opted for, you should now find Flickity is immediately functioning with the defaults setup and properly laid out.

In our case, the defaults include arrows to navigate, dots which represent the number of items in the slider, and we can also flick through each slide with a swipe gesture. If you are using a laptop, you can drag the slide with three-finger gesture on the trackpad.

Nice!

Step 3: Customizing the Options

In our previous demo, you will see a wide empty space that spans from the left arrow to the first slide, as well as another at the end of the last slide. 

This is Flickity default behaviour when the slides are not 100% the width of the parent container. This could look really weird on the website, therefore we want the slides to span across the empty space. 

Additionally, I want to be able to slide the slider indefinitely. To do so, we need to enable the wrapAround option.

Tip: it is better not to use margin-right and margin-left to add spaces between each slide as the Flickity slide position could be inperfect, giving an undesirable result. Instead, use padding or add the margin to the child element to make up the space between the slide.

Margin and Padding illustration

Proper use of margin and padding to add space between the slide.

Going Dotty

I once saw a situation where someone had added 50 slides, along with the dots which were then stacked up to three rows. For this reason, these days I tend to hide dot navigation at the bottom of any slider. You might want to consider disabling the dot navigation as well, especially when you are going to hand over the website to clients. To hide the dots, set the pageDots to false.

Here’s what that gives us:

Flickity provides an abundance of Options. Find them in more details with examples on the Options page

Step 5: Adding a Flourish with VelocityJS

Though the slider is functioning, it looks pretty static. Let’s give it some flourish with an animation using VelocityJS and its UI Packs. Begin by linking both the Velocity.js and the UI Packs library:

Then, paste the following script.

Now, as we load the page the slide will zoom in sequentially one after another. If the animation is not your thing, feel free to change the transition. There are loads of different animation in the UI Packs.

Hint: Rerun the demo:

Step 6: What if JavaScript is Disabled?

One last thing. We need a fallback, to make sure that the content is still well presented even if JavaScript is disabled. Fallbacks of this nature depend on the content. In our case, the content should be properly laid out in a grid…

…which is as easy as floating all the slides to the left.

Conclusion

Nowadays, you will find one or two sliders in the majority of websites. Their usefulness when it comes to driving conversion is up for debate, but many of your clients will still insist you add it to their website. When that’s case, consider using Flickity. It is fairly easy to use, fully customizable, performant, and responsive out-of-the-box.