Trigger events and animations while scrolling your page.

\n\n

Usage

\n\n

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.

\n\n
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
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.
\n
\n\n

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

\n\n

Examples

\n\n

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

\n\n
\n
\n
\n

Fade

\n

Lorem ipsum dolor sit amet, consectetur adipisicing elit.

\n
\n
\n
\n\n
\n
\n
\n

Scale up

\n

Lorem ipsum dolor sit amet, consectetur adipisicing elit.

\n
\n
\n
\n\n
\n
\n
\n

Scale down

\n

Lorem ipsum dolor sit amet, consectetur adipisicing elit.

\n
\n
\n
\n\n
\n
\n
\n

Slide top

\n

Lorem ipsum dolor sit amet, consectetur adipisicing elit.

\n
\n
\n
\n\n
\n
\n
\n

Slide bottom

\n

Lorem ipsum dolor sit amet, consectetur adipisicing elit.

\n
\n
\n
\n\n
\n
\n
\n

Slide right

\n

Lorem ipsum dolor sit amet, consectetur adipisicing elit.

\n
\n
\n
\n\n
\n
\n
\n

Slide left

\n

Lorem ipsum dolor sit amet, consectetur adipisicing elit.

\n
\n
\n
\n\n
\n
\n
\n

Delay

\n

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

\n
\n
\n
\n
\n

Delay

\n

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

\n
\n
\n
\n
\n

Delay

\n

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

\n
\n
\n
\n
\n

Delay

\n

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

\n
\n
\n
\n\n

Markup

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

JavaScript options

\n\n

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

\n\n
data-uk-scrollspy="{cls:'uk-animation-fade'}"
\n\n
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
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
\n
\n\n\n

Events

\n\n

You can bind callbacks to following events for custom functionality:

\n\n
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
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
\n
\n\n

Example

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

Scrollspy Nav

\n\n

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.

\n\n
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
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 by 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.
\n
\n\n

Markup

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

JavaScript options

\n\n

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

\n\n
data-uk-scrollspy-nav="{smoothscroll:true}"
\n\n
\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
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
\n
\n\n\n

Events

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