Getting Started
Butr is a tiny (3kb~) JavaScript package that can:
- Collect all headings and create a table of contents with links
- Update the table of contents to indicate which link is currently in view
- Smoothly animate scroll to any location or element - like butter ;)
- Automatically animate scroll on any anchor with the
data-butr
attribute
The best example is this documentation itself, which uses butr for its sidebar.
Issues
Please create an issue on GitHub if you find any bugs or have a suggestion.
Browser Support
Butr works well on all modern browsers and IE 10+.
Downloading or Installing
Stand Alone
- Go to the latest release and download
butr.min.js
Include it somewhere before the closing
</body>
tag:<script src="path/to/butr.min.js"></script>`
Import or Require
Install with npm
npm i --save butrES2015
import butr from 'butr' // ES2015+ onlyrequireJS
var butr = require('butr') // requireJS only
Usage
Butr has 5 methods that can all be called independently or all together. Some methods depend on having specific classes in your markup. Check each method for required classes.
Full Example
This snippet will create a table of contents with links, from the headings in your content with butr.autoSidebar()
, create a marker to track which link in the table of contents is in view with butr.marker()
, pin the table of contents to the top of the page after scrolling with butr.stickyNav()
, and animate scroll of any anchor tags with the data-butr
atrribute using butr.autoAnchors()
.
Keep in mind that order of operations is important between some methods. .autoAnchors()
should always come last so that it can attach events to any dynamically generated elements. The sidebar (created with .autoSidebar()
or any other means) should be created before attaching a marker with .marker()
.
JavaScript
|
HTML
|
.to()
Animate scrolling to a location or element.
Scroll to element
|
Scroll to location
|
All options
|
.autoAnchors()
Automatically smooth scroll to any anchor’s href
with the data attribute data-butr
.
JavaScript
|
HTML
|
.autoSidebar()
Automatically create a table of contents with links based on the headings in the content.
JavaScript
|
HTML
|
.marker()
Track which nav link’s section is currently in view. Optionally style a marker to further indicate which section is in view. The marker on this page is a good example of what is possible. Use CSS to do basically anything with it.
JavaScript
|
HTML
If you are manually creating nav links, make sure to add the js-butr-link
class so they are picked up by .marker()
. If you are using .autoSidebar()
they’re picked up automatically.
|
.stickyNav()
Pin the sidebar nav to the top of the page once the top of it hits the top of the browser when scrolling.
JavaScript
|
HTML
|