How to use
Copy-paste the following <script>
near the end of your pages under JS Implementing Plugins to enable it.
<script src="../assets/vendor/hs-scrollspy/dist/hs-scrollspy.min.js"></script>
Copy-paste the init function under JS Plugins Init., before the closing </body>
tag, to enable it.
<script>
$(document).on('ready', function () {
// initialization of scroll nav
var scrollspy = new HSScrollspy($('body'), {
// !SETTING "resolve" PARAMETER AND RETURNING "resolve('completed')" IS REQUIRED
beforeScroll: function(resolve) {
if (window.innerWidth < 992) {
$('#navbarVerticalNavMenu').collapse('hide').on('hidden.bs.collapse', function () {
return resolve('completed');
});
} else {
return resolve('completed');
}
}
}).init();
});
</script>
How it works
Scrollspy has a few requirements to function properly:
- Scrollspy requires
data-hs-scrollspy-options='{ "target": "#yourID" }'
on<body>
element and must point to an element with the targetID
. And also, add the.js-scrollspy
class to the element where thisID
is placed. - Scrollspy requires
position: relative;
on the element, you’re spying on, usually the<body>
. - When spying on elements other than the
<body>
, be sure to have aheight
set andoverflow-y: scroll;
applied. - Anchors (
<a>
) are required and must point to an element with thatID
.
When successfully implemented, your nav or list group will update accordingly, moving the .active
class from one item to the next based on their associated targets.
Example
This example uses Sticky block library.
Methods
Parameters | Description | Default value |
---|---|---|
|
Сontainer inside which scroll will be | 'html, body' |
|
Scroll speed | 400 |
|
Easing functions specify the speed at which an animation progresses at different points within the animation. https://api.jqueryui.com/easings/ | 'linear' |
|
Pixels to offset from top when calculating position of scroll. | 0 |
|
Сontainer inside which scroll will be | 'html, body' |
|
Finds which section the spied element is in. auto will choose the best method to get scroll coordinates. offset will use jQuery offset method to get scroll coordinates. position will use jQuery position method to get scroll coordinates. | null |
|
Specifies element to apply Scrollspy plugin. | 'auto' |
|
Callback function | null |
|
Callback function | null |