7 days of unlimited WordPress themes, plugins & graphics - for free!* Unlimited asset downloads! Start 7-Day Free Trial
  1. Web Design
  2. UIKit

How to Create a Scrollspy Navigation With UIkit

Read Time: 6 mins

There are different ways to apply scrolling animations in a project. In the past, we’ve covered many of them. For example, we’ve built a project that uses the Intersection Observer API, another one that uses AOS.js, and another one that uses a short JS snippet. Today, we’ll build a page with scrolling animations by taking advantage of UIkit’s Scrollspy component

Note: similarly to another recent UIkit tutorial, although not required, some knowledge of this framework will be beneficial. Even if you've never used it, I encourage you to give this tutorial a chance. Think of it as a way to learn one or two things about a new framework and how to solve easily common UI tasks with it.

Our Scroll Effect

Here’s what we're going to create:

For a better experience, be sure to view the page on a large screen and notice two things:

  • How the image captions smoothly appear when they come into view.
  • How the active menu item of the side navigation changes depending on the scroll position.

UIKit Templates on Envato Elements

Before we start, don’t forget to check out the various HTML templates built with UIKit on Envato Elements, like Freda Personal Resume. On Envato Elements you’ll find millions of creative assets, all ready to use, and with simple commercial licensing. Plus no lock-in contract means you can cancel any time!

Freda Personal Resume / Portfolio / HTML TemplateFreda Personal Resume / Portfolio / HTML TemplateFreda Personal Resume / Portfolio / HTML Template

And now, on with our own UIkit tutorial!

1. Begin With the Required Assets

To make the layout a bit more unique, beyond the required UIkit files, we’ll also include some handmade SVG illustrations. More specifically:

  • An SVG that will replace the typical bullets of the menu items:
The first handmade SVG
  • An SVG that will denote the active menu item on scroll:
The second handmade SVG
  • An SVG for styling the image captions that comes from a previous tutorial. Have a look at it as we dive into different positioning techniques.
The third handmade SVG

2. Define the HTML Markup

Our page will consist of the following elements:

  • Five sections with beautiful Icelandic images taken from Unsplash along with their captions. Each caption will appear with a smooth fade-in animation as it enters the viewport thanks to the uk-scrollspy attribute. This attribute can receive different parameters for customizing the animation. For example, we’re able to pass parameters for deciding if the animation should run every time the element is in view, or if there will be any delay until it appears.
  • The side navigation menu. On this element, we’ll use the uk-scrollspy-nav attribute for updating the active menu item depending on the scroll position of our page. To relate a section with a menu item, each menu item should link to the ID of an existing section. Again, we can customize the scrollspy nav by passing an object to the uk-scrollspy-nav attribute.

Additionally, it’s worth mentioning that to build the layout we’ll heavily take advantage of UIkit’s different helper classes, attributes, and components. For consistency reasons, all UIkit classes that start with the uk- prefix will follow any custom ones. Most of the framework classes are self-explanatory but if you need any clarification be sure to read the documentation or open the browser console. Lastly, be sure to check out the Image component to understand how the lazy loading works.

Here’s the corresponding markup:

3. Specify the Main Styles

As we’ve seen, the markup includes various utility classes. Given this fact, the CSS will be as minimal as possible.

Most importantly, we’ll define some styles for toggling the visibility of all nav elements on hover and changing the SVG icon of the active menu item.

The toggle state of the navigation

Here are all the page styles:

4. Add the JavaScript

By default, the page navigation will be a fixed positioned element without having a static height. So far, the nav items are only five. But what will happen if they grow? In this case, we should take into account the screen height and decide whether a vertical scrollbar will appear inside the nav or not.

For example, here’s how the navigation with look with ten items:

The navigation appearance with 10 items

And here’s with 14 items:

The navigation appearance with 14 items

In this case, the nav will have a 50px gap between the top and bottom parts of the screen.

Here’s the required JavaScript code:


Done! Today we took advantage of UIkit's Scrollspy component and managed to build a useful page scroll effect. Not bad without much effort! Feel free to extend it by using either the Parallax component or writing some custom code for updating the page URL without forcing a page to reload, etc.

Let’s look again at what we built:

As always, thanks a lot for reading!

More UIkit Tutorials

Learn more about this popular frontend framework on Tuts+.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Scroll to top
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.