Framework-agnostic scrollspy script, fork of Gumshoe by cferdinandi. The latest compiled release can be found in the dist
directory.
What's different?
Scrollmus supports all modern browsers; this does not include Internet Explorer.
Firefox | Chrome | Edge | Safari | IE |
---|---|---|---|---|
35+ | 41+ | 15+ | 9+ | -- |
Download the latest compiled release from the dist
directory.
<script defer src="/path/to/scrollmus.min.js"></script>
Scrollmus can be loaded as a Hugo Module.
Initialize the Hugo project as a Hugo Module. Replace the path with your repository.
hugo mod init gitlab.com/username/project
Add the following [module]
configuration to the project's config.toml
file. If the gitlab.com
import fails for your project, replace it with github.com
. Scrollmus is distributed on both platforms.
[module]
[[module.imports]]
path = 'gitlab.com/aao-fyi/scrollmus'
[[module.imports.mounts]]
source = 'dist/'
target = 'assets/js/scrollmus/'
Load Scrollmus wherever necessary.
{{ $scrollmus := resources.Get "js/scrollmus/scrollmus.min.js" }}
<script defer src="{{ $scrollmus.RelPermalink }}"></script>
Optionally use subresource integrity.
{{ $scrollmus := resources.Get "js/scrollmus/scrollmus.min.js" }}
{{ $scrollmusSRI := $scrollmus | resources.Fingerprint "sha384" }}
<script defer src="{{ $scrollmusSRI.RelPermalink }}" integrity="{{ $scrollmusSRI.Data.Integrity }}"></script>
The wombats of the group can use NPM.
npm install scrollmus
The only thing Scrollmus needs to work is a list of anchor links. They can be ordered or unordered, inline or unstyled, or even nested.
<ul id="my-awesome-nav">
<li><a href="#eenie">Eenie</a></li>
<li><a href="#meenie">Meenie</a></li>
<li><a href="#miney">Miney</a></li>
<li><a href="#mo">Mo</a></li>
</ul>
In the footer of your page, after the include, initialize Scrollmus by passing in a selector for the navigation links that should be detected as the user scrolls.
<script>
var spy = new Scrollmus('#my-awesome-nav a');
</script>
Scrollmus adds the .active
class to the list item (<li></li>
) and content for the active link, but does not include any styling.
Add styles to your CSS as desired. And that's it, you're done. Nice work!
#my-awesome-nav li.active a {
font-weight: bold;
}
Note: you can customize the class names with user options.
Scrollmus includes smart defaults and works right out of the box. But if you want to customize things, it also has a robust API that provides multiple ways for you to adjust the default options and settings.
You can pass options into Scrollmus when instantiating.
var spy = new Scrollmus('#my-awesome-nav a', {
// Active classes
navClass: 'active', // applied to the nav list item
contentClass: 'active', // applied to the content
// Nested navigation
nested: false, // if true, add classes to parents of active link
nestedClass: 'active', // applied to the parent items
// Offset & reflow
offset: 0, // how far from the top of the page to activate a content area
reflow: false, // if true, listen for reflows
// Event support
events: true, // if true, emit custom events
// End of page
useLast: true // if true, the last page item will be set as 'active' when scrolled to bottom
});
Scrollmus emits two custom events:
scrollmusActivate
is emitted when a link is activated.scrollmusDeactivate
is emitted when a link is deactivated.
Both events are emitted on the list item and bubble up. You can listen for them with the addEventListener()
method. The event.detail
object includes the link
and content
elements, and the settings
for the current instantiation.
// Listen for activate events
document.addEventListener('scrollmusActivate', function (event) {
// The list item
var li = event.target;
// The link
var link = event.detail.link;
// The content
var content = event.detail.content;
}, false);
Scrollmus also exposes several public methods.
Setups all of the calculations Scrollmus needs behind-the-scenes. If you dynamically add navigation items to the DOM after Scrollmus is instantiated, you can run this method to update the calculations.
var spy = new Scrollmus('#my-awesome-nav a');
spy.setup();
Activate the navigation link that's content is currently in the viewport.
var spy = new Scrollmus('#my-awesome-nav a');
spy.detect();
Destroy the current instance of Scrollmus.
var spy = new Scrollmus('#my-awesome-nav a');
spy.destroy();
If you have a nested navigation menu with multiple levels, Scrollmus can also apply an .active
class to the parent list items of the currently active link.
<ul id="my-awesome-nav">
<li><a href="#eenie">Eenie</a></li>
<li>
<a href="#meenie">Meenie</a>
<ul>
<li><a href="#hickory">Hickory</a></li>
<li><a href="#dickory">Dickory</a></li>
<li><a href="#doc">Doc</a></li>
</ul>
</li>
<li><a href="#miney">Miney</a></li>
<li><a href="#mo">Mo</a></li>
</ul>
Set nested
to true
when initializing. This class name can also be customized.
var spy = new Scrollmus('#my-awesome-nav a', {
nested: true,
nestedClass: 'active-parent'
});
If the content that's linked to by your navigation has different layouts at different viewports, Scrollmus will need to detect these changes and update some calculations behind-the-scenes.
Set reflow
to true
to enable this (it's off by default).
var spy = new Scrollmus('#my-awesome-nav a', {
reflow: true
});
If you have a fixed header on your page, you may want to offset when a piece of content is considered "active."
The offset
user setting accepts either a number, or a function that returns a number. If you need to dynamically calculate dimensions, a function is the preferred method.
Here's an example that automatically calculates a header's height and offsets by that amount.
// Get the header
var header = document.querySelector('#my-header');
// Initialize Scrollmus
var spy = new Scrollmus('#my-awesome-nav a', {
offset: function () {
return header.getBoundingClientRect().height;
}
});
By default, when the user scrolls to the bottom of the page the last item will be marked active. To prevent this behavior, set useLast
to false when you call Scrollmus. When useLast
is false, the item at the top of the page will continue to be marked active.
var spy = new Scrollmus('#my-awesome-nav a', {
useLast: false
});
Open new issues in the GitLab Issue Tracker.
Scrollmus is distributed under the MIT License.