Anchorific is a jQuery plugin that automatically generates anchored headings and nested navigations based on header tags. My intention is for it to be used in single-page project documentation. Project page and demo here: https://anchorific.netlify.app/
npm install --save anchorific
For a guide on how to using jQuery plugins with npm, check out: https://blog.npmjs.org/post/112064849860/using-jquery-plugins-with-npm
You should not skip a level when structuring header tags. H1 should be followed by H2, H2 should be followed by H3 and so on. Anchorific relies heavily on this particular structure when generating the anchor navigation.
The LannistersTywin LanisterCersei LannisterJoffrey BaratheonMyrcella BaratheonTommen BaratheonJaime LannisterTyrion Lannister
Based on the HTML markup above, the plugin will generate nested navigations like this one:
The LannistersTywin LannisterCersei LannisterJoffrey BaratheonMyrcella BaratheonTommen BaratheonJaime LannisterTyrion Lannister
...and it will generate anchored headings like this one:
Tywin Lannister<!-- This would be turn to -->Tywin Lannister #
Any existing ID will be preserved by the plugin:
What about existing ID?<!-- This would be turn to -->What about existing ID?#
Include a div or a nav section where you want the unordered list of anchor navigation to be appended at:
By default, the plugin will append the unordered list under an element with class called anchorific. If you wish to use another class name, you need to specify it in the plugin's option.
The nested navigation can be styled easily. Below are the selectors you can use in order to style the generated navigation.
/* active class is generated by the scrollspy */
You can also check the demo page's CSS to see how it is done.
Basic Usage and Options
Use the selector where your headings are located under. And then just call the anchorific method.
You can call the plugin function with any selector you want as long as it adhere to the HTML structure mentioned above. Options available are as followed:
Generating navigations, Scroll spy, and 'Back to top' functionality can be disable by assigning false value to the options.
Adding 'Back to Top' Button
Just add an element with class top. You can use other class names but it should be specified in the plugin options.
Scroll to top
The speed of the scrolling effect can be adjusted by specifying it in the options above.
Note: remember to add display: none; to the .top styling. It should not be visible when the page first load.
Anchorific.js good old npm scripts to run test and build the code.
$ npm test
$ npm run build
CD to the project folder and type:
$ npm install
In order to contribute, open a pull request and ensure that new tests are written in order to test out your contributions. Ensure that previous test pass by running
npm test. Running
/tests/index.html directly in the browser will show you some failed tests but this is because those tests are testing the scroll functionality which it doesn't support. Running
npm test is more reliable.