After getting tired of adding a snippit of code in the JavaScript of my web projects to enable smoothly animated, on-page navigation, I decided to put together a library so this functionality can be enabled through the html5 data-api
or through a simple jQuery
extension. I hope you can find a use for it!
I treated this project as an exercise in following the coding style and practice I find in the Bootstrap project. I have great respect for @mdo and @fat and the Bootstrap team for the awesome frameworks and advice they provide to the community. Rock on guys!
This package requires JQuery >= 1.7
to function.
Install smooth-nav with npm by using npm install smooth-nav
To add smooth-nav functionality to a link, add data-scroll="smooth"
to any anchor tag which references an on page element.
<a href="#a-cool-place" data-scroll="smooth">Go to</a>
...
<div id="a-cool-place">
<h1>The Cool Place</h1>
</div>
Smooth-nav links must target a element which can be resolved. For example a <a href="#the-target">Link</a>
must point to an element on the page like <div id="the-target"></div>
.
Call the smooth-nav plugin via JavaScript:
$('#link').smooth({ speed: 250 });
Options can be set via the data-api or JavaScript. To use the data-api, append the option name to data-scroll-
.
Name | Type | Default | Description |
---|---|---|---|
speed | number | 300 | The speed in milliseconds at which the scroll animation plays |
You are living the example right now! Use links on this page to experience smooth-nav in action.
Dumping the license to exaggerate the effect.