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 attribute | Description |
---|---|
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'}"
Option | Possible value | Default | Description |
---|---|---|---|
cls | string | ‘uk-scrollspy-inview’ | Class to add when element is in view |
initcls | string | ‘uk-scrollspy-init-inview’ | Class to add when element is in view for first time |
topoffset | integer | 0 | Top offset before triggering in view |
leftoffset | integer | 0 | Left osset before triggering in view |
repeat | boolean | false | Applies the in view class everytime the element appears in the viewport. |
delay | integer | 0 | Delay time in ms |
Events
You can bind callbacks to following events for custom functionality:
Name | Description |
---|---|
uk.scrollspy.init | Triggered when the element is initially in the viewport |
uk.scrollspy.inview | Triggered when the element is in the viewport |
uk.scrollspy.outview | Triggered 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 attribute | Description |
---|---|
data-uk-scrollspy-nav | Triggers 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}"
Option | Possible value | Default | Description |
---|---|---|---|
cls | string | ‘uk-active’ | Class to add to active element |
closest | CSS selector | false | Element selector to aply the active class to |
topoffset | integer | 0 | Sroll top-offsset |
leftoffset | integer | 0 | Sroll left-offsset |
smoothscroll | boolean | false | Scroll animation |
Events
Name | Parameter | Description |
---|---|---|
init.uk.scrollspy | event | On scrollspy first init |
inview.uk.scrollspy | event | Element entered view-port |
outview.uk.scrollspy | event | Element left view-port |