Interacting with the Webflow Slider Component using Alpine.js

Jarek Lipski
Webflow Sprinkles
Published in
5 min readSep 14, 2021

A real-world example.

Photo by Philipp Lublasser on Unsplash

Webflow components, such as Slider, Tabs or Lightbox, are very powerful, but their internal structure can sometimes be too rigid to style them as desired. You may also need to combine multiple components together. To solve these problems, you need to write a bit of glue code. You can do this using imperative JavaScript or jQuery, but you can achieve the same effect with reactive Alpine.js code.

In my previous article, we explored how to use basic Alpine.js features inside Webflow. In this one, you will develop an alternative navigation bar for the Slider component, as seen at the bottom of the picture below:

TL;DR

If you are in a rush and just want to see an example of how to interact with the Webflow component in Alpine.js, visit my demo site and check out the example on the Components page:

To see the code, visit this Read-only link.

Getting started

Initializing Alpine.js

To access Alpine.js in Webflow, you can use my library:

To initialize it, include the following script tag at the end of the <body> element, either globally in Project Settings or via HTML Embed on each page:

<script src="https://cdn.jsdelivr.net/npm/@loomchild/webflow-alpinejs@2/dist/index.js"></script>

Please refer to my article How to add custom code to Webflow to learn more about custom code in Webflow.

Next, add the HTML Embed containing the following style tag at the beginning of the <body> element on each page:

<link href="https://cdn.jsdelivr.net/npm/@loomchild/webflow-alpinejs@2/dist/style.css" rel="stylesheet">

Adding Alpine.js directives via custom attributes

Alpine.js directives, used to add behavior to your website, are declared as custom element attributes. To add a custom attribute to an element in Webflow Designer, go to the Element Settings tab and click the plus icon on the custom attributes panel:

In this article, custom attributes will be described as follows:

attribute-name="attribute-value"

which corresponds to the following settings in Webflow Designer:

Building the Slider navigation bar

Now you are ready to interact with the Webflow Slider component and build an alternative navigation bar like the one on this animation:

After completing this tutorial, you should end up with the following element structure in Webflow Designer:

1. Create the Slider

Create a Slider component via the Add elements menu and add a few slides with image backgrounds to it:

You should see something similar to the below image in Designer:

2. Initialize the Alpine.js slider component

To interact with Webflow Slider, there’s a slider component available in the webflow-alpinejs library. To initialize this component, wrap the Slider in a Slider Wrapper div block and apply the x-data directive to it:

x-data="slider"

3. Add previous and next buttons

Now let’s create a navigation bar with next < and previous > buttons:

Create the Slider Navigation Bar div block element below the Slider and inside the Slider Wrapper, and add two Slide Link elements to it:

Apply the following x-on:click directive to < button, to go back to the previous slide by calling the previousSlide function provided by the Alpine.js slider component:

x-on:click="previousSlide"

Conversely, apply the following x-on:click directive to > button, to advance to the next slide:

x-on:click="nextSlide"

4. Add slide selection buttons

Finally, let’s add the buttons that will switch directly to a specific slide:

To avoid repetition, let’s use a loop. Create a Slide Links div block element inside the Slider Navigation Bar and add a single Slide Link element to it:

Apply the following x-for and x-bind:key directives to Slide Links:

x-for="(item, index) in ['Winter', 'Spring', 'Summer', 'Autumn']"
x-bind:key="item"

And apply the following directives to the Slide Link element inside Slide Links:

x-text="item"
x-on:click="slide = index"
x-bind:class="{ active: slide === index }"

The first directive displays the current slide name, the second directive switches the slide in reaction to click, and the third directive highlights the active link.

After deployment, the page should look and work as shown in the animation at the beginning of this chapter.

Conclusion

As you can see, interacting with the native Webflow component in Alpine.js can be quite intuitive thanks to reusable Alpine.js components. This allows you to focus on writing clean, responsive code, without worrying about writing the code to glue the two technologies together.

How does it work?

If you are curious how the Alpine.js component is implemented, do not hesitate to check out the webflow-alpinejs library source code. I promise that it’s pretty straightforward, provided you know the powerful MutationObserver JavaScript API.

--

--

Jarek Lipski
Webflow Sprinkles

Freelance Full-Stack Developer | Technoblast @ Tech for Bio