Scrollspy
Materialize is a modern responsive CSS framework based on Material Design by Google.
Scrollspy tracks certain elements and which element the user's screen is currently centered on. Our main demo of this is our table of contents on every documentation page to the right. Clicking on these links will also scroll the page to that element.
Content
Content
Content
Initialization
document.addEventListener('DOMContentLoaded', function() {
const elems = document.querySelectorAll('.scrollspy');
const instances = M.ScrollSpy.init(elems, {
// specify options here
});
});
Options
Name | Type | Default | Description |
---|---|---|---|
throttle | Number | 100 | Throttle of scroll handler. |
scrollOffset | Number | 200 | Offset for centering element when scrolled to. |
activeClass | String | 'active' | Class applied to active elements. |
getActiveElement | Function | Used to find active element. |
getActiveElement
This is the default function used for finding the active element where id is the id of the scrollspy element. It returns a CSS selector of the element you want marked active.
function(id) {
return 'a[href="#' + id + '"]';
}
Methods
All the methods are called on the plugin instance. You can get the plugin instance like this:
const instance = M.ScrollSpy.getInstance(elem);
.destroy();
Destroy plugin instance and teardown
instance.destroy();
Properties
Name | Type | Description |
---|---|---|
el | Element | The DOM element the plugin was initialized with. |
options | Object | The options the instance was initialized with. |