Scrollspy

Trigger events and animations while scrolling your page.

Usage

The scrollspy component listens to page scrolling and triggers events based on the scroll position. For example, if you scroll down a page and an element appears the first time in the viewport you can trigger a smooth animation to fade in the element.

Data attributeDescription
data-uk-scrollspy="{cls:'MY-CLASS'}"Applies the class only the first time the element appears in the viewport.
data-uk-scrollspy="{cls:'MY-CLASS', repeat: true}"Applies the class everytime the element appears in the viewport.
data-uk-scrollspy="{cls:'MY-CLASS', delay:600}"Adds a delay in milliseconds to the animation.

Typically, classes from the Animation component are used together with the scrollspy.

Examples

This example uses the repeat: true option. Scroll up and down to see the triggered animations.

Fade

Lorem ipsum dolor sit amet, consectetur adipisicing elit.

Scale up

Lorem ipsum dolor sit amet, consectetur adipisicing elit.

Scale down

Lorem ipsum dolor sit amet, consectetur adipisicing elit.

Slide top

Lorem ipsum dolor sit amet, consectetur adipisicing elit.

Slide bottom

Lorem ipsum dolor sit amet, consectetur adipisicing elit.

Slide right

Lorem ipsum dolor sit amet, consectetur adipisicing elit.

Slide left

Lorem ipsum dolor sit amet, consectetur adipisicing elit.

Delay

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Delay

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Delay

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Delay

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Markup

<div data-uk-scrollspy="{cls:'uk-animation-fade'}">...</div>

<div data-uk-scrollspy="{cls:'uk-animation-fade', repeat: true}">...</div>

<div data-uk-scrollspy="{cls:'uk-animation-fade', delay:900}">...</div>

JavaScript options

This is an example of how to set options via attribute:

data-uk-scrollspy="{cls:'uk-animation-fade'}"
OptionPossible valueDefaultDescription
clsstring‘uk-scrollspy-inview’Class to add when element is in view
initclsstring‘uk-scrollspy-init-inview’Class to add when element is in view for first time
topoffsetinteger0Top offset before triggering in view
leftoffsetinteger0Left osset before triggering in view
repeatbooleanfalseApplies the in view class everytime the element appears in the viewport.
delayinteger0Delay time in ms

Events

You can bind callbacks to following events for custom functionality:

NameDescription
uk.scrollspy.initTriggered when the element is initially in the viewport
uk.scrollspy.inviewTriggered when the element is in the viewport
uk.scrollspy.outviewTriggered when the element leaves the viewport

Example

$('[data-uk-scrollspy]').on('uk.scrollspy.inview', function(){
    // custom code...
});

Scrollspy Nav

To automatically update the active menu item in a menu depending on the scroll position of your site, just add the data attribute data-uk-scrollspy-nav to any navigation. Each menu item must link to the ID of its corresponding part of the site.

Data attributeDescription
data-uk-scrollspy-navTriggers the JavaScript necessary for the functionality of the scrollspy nav.
data-uk-scrollspy-nav="{closest:'MY-SELECTOR'}"Looks for the closest element that matches the selector por testing the element itself and traversing up through its ancestors in the DOM tree.
data-uk-scrollspy-nav="{smoothscroll:true}"Applies the Smooth scroll component when skipping between different sections of the site.
data-uk-scrollspy-nav="{cls:'MY-CLASS'}"By default ScrollspyNav toggles the uk-active class. Use the cls option to use your own class name.

Markup

<ul class="uk-nav uk-nav-side" data-uk-scrollspy-nav="{closest:'li', smoothscroll:true}">
    <li><a href="#MY-ID">...</a></li>
    <li><a href="#MY-ID2">...</a></li>
</ul>

<div id="MY-ID">...</div>
<div id="MY-ID2">...</div>

JavaScript options

This is an example of how to set options via attribute:

data-uk-scrollspy-nav="{smoothscroll:true}"
OptionPossible valueDefaultDescription
clsstring‘uk-active’Class to add to active element
closestCSS selectorfalseElement selector to aply the active class to
topoffsetinteger0Sroll top-offsset
leftoffsetinteger0Sroll left-offsset
smoothscrollbooleanfalseScroll animation

Events

NameParameterDescription
init.uk.scrollspyeventOn scrollspy first init
inview.uk.scrollspyeventElement entered view-port
outview.uk.scrollspyeventElement left view-port