How to Create a Scrollspy Navigation With UIkit

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:

Conclusion

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+.


This content originally appeared on Envato Tuts+ Tutorials and was authored by George Martsoukos

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:

Conclusion

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+.


This content originally appeared on Envato Tuts+ Tutorials and was authored by George Martsoukos


Print Share Comment Cite Upload Translate Updates
APA

George Martsoukos | Sciencx (2021-12-29T11:32:56+00:00) How to Create a Scrollspy Navigation With UIkit. Retrieved from https://www.scien.cx/2021/12/29/how-to-create-a-scrollspy-navigation-with-uikit/

MLA
" » How to Create a Scrollspy Navigation With UIkit." George Martsoukos | Sciencx - Wednesday December 29, 2021, https://www.scien.cx/2021/12/29/how-to-create-a-scrollspy-navigation-with-uikit/
HARVARD
George Martsoukos | Sciencx Wednesday December 29, 2021 » How to Create a Scrollspy Navigation With UIkit., viewed ,<https://www.scien.cx/2021/12/29/how-to-create-a-scrollspy-navigation-with-uikit/>
VANCOUVER
George Martsoukos | Sciencx - » How to Create a Scrollspy Navigation With UIkit. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/29/how-to-create-a-scrollspy-navigation-with-uikit/
CHICAGO
" » How to Create a Scrollspy Navigation With UIkit." George Martsoukos | Sciencx - Accessed . https://www.scien.cx/2021/12/29/how-to-create-a-scrollspy-navigation-with-uikit/
IEEE
" » How to Create a Scrollspy Navigation With UIkit." George Martsoukos | Sciencx [Online]. Available: https://www.scien.cx/2021/12/29/how-to-create-a-scrollspy-navigation-with-uikit/. [Accessed: ]
rf:citation
» How to Create a Scrollspy Navigation With UIkit | George Martsoukos | Sciencx | https://www.scien.cx/2021/12/29/how-to-create-a-scrollspy-navigation-with-uikit/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.