preface
Resource introduction
<script src=" https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.5.0/tocbot.min.js "></script> <link rel="stylesheet" href=" https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.5.0/tocbot.css ">
Build Directory
Title Anchor
<h1 id="TOC build toc">Build Directory</h1> <h2 id="TOC test">Test</h2> <!-- ... -->
#Heading 1 ##Heading 2 ###Title 3
Some considerations:
It is better not to have symbols or other special characters in the title ID Add an index number to the repeated id to distinguish
var headerEl = 'h1,h2,h3,h4', //headers Content='. post content',//article container idArr = {}; // Header array to determine whether to add index id //add #id $(content).children(headerEl).each(function () { //Remove spaces and extra punctuation var headerId = $(this).text().replace(/[\s|\~|`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\_|\+|\=|\||\|\[|\]|\{|\}|\;|\:|\ "|\'|\,|\<|\.|\>|\/|\?|\:|\,|\。]/g, ''); headerId = headerId.toLowerCase(); if (idArr[headerId]) { //ID already exists $(this).attr('id', headerId + '-' + idArr[headerId]); idArr[headerId]++; } else { //ID does not exist idArr[headerId] = 1; $(this).attr('id', headerId); } });
initialization
tocbot.init({ //Container for building directory tocSelector: '.js-toc', //Article Container contentSelector: '.js-toc-content', //Title to be resolved headingSelector: 'h1, h2, h3', });
var headerEl = 'h1,h2,h3,h4', //headers Content='. post content',//article container idArr = {}; // Header array to determine whether to add index id tocbot.init({ tocSelector: '.toc', contentSelector: content, headingSelector: headerEl, //positionFixedSelector: '.toc', //positionFixedClass: 'is-position-fixed', //fixedSidebarOffset: 'auto', scrollSmooth: true, scrollSmoothOffset: -80, headingsOffset: -500 });
PJAX callback
$(document).on('pjax:send',function(){ //Destroy () method if ($('.toc').length) tocbot.destroy(); }) $(document).on('pjax:complete',function(){ //Call the toc. init() method again //For example toc.init(tocOptions); })
Advanced
Catalog Scroll Follow
positionFixedSelector - Element to add the positionFixedClass to.
positionFixedClass - Fixed position class to add to make sidebar fixed after scrolling down past the fixedSidebarOffset.
fixedSidebarOffset - fixedSidebarOffset can be any number but by default is set to auto which sets the fixedSidebarOffset to the sidebar element's offsetTop from the top of the document on init.
positionFixedSelector: '.toc', positionFixedClass: 'is-position-fixed', fixedSidebarOffset: 'auto',
.is-position-fixed { position:fixed !important; top:0 }
"This is an experimental API. Please try not to use it in the production environment."
Jump offset
use
scrollEndCallback The configuration item manually writes the code for the offset after the jump use
scrollSmoothOffset This configuration parameter
Rolling monitoring
When scrolling to the corresponding title, the directory will listen to the current position to highlight/highlight the title in the directory. But if the two titles are too close, or you have offset the jump, then The title highlighted in the table of contents may not correspond to the title in the article 。
if (heading.offsetTop > top + options.headingsOffset + 10) { // Don't allow negative index value. var index = (i === 0) ? i : i - 1 topHeader = headings[index] return true } else if (i === headings.length - 1) { // This allows scrolling for the last heading on the page. topHeader = headings[headings.length - 1] return true }
Code Reference
if ($('.toc').length > 0) { var headerEl = 'h1,h2,h3,h4', //headers Content='. post content',//article container idArr = {}; // Header array to determine whether to add index id //add #id $(content).children(headerEl).each(function () { //Remove spaces and extra punctuation var headerId = $(this).text().replace(/[\s|\~|`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\_|\+|\=|\||\|\[|\]|\{|\}|\;|\:|\ "|\'|\,|\<|\.|\>|\/|\?|\:|\,|\。]/g, ''); headerId = headerId.toLowerCase(); if (idArr[headerId]) { //ID already exists $(this).attr('id', headerId + '-' + idArr[headerId]); idArr[headerId]++; } else { //ID does not exist idArr[headerId] = 1; $(this).attr('id', headerId); } }); tocbot.init({ // Where to render the table of contents. tocSelector: '.toc', // Where to grab the headings to build the table of contents. contentSelector: content, // Which headings to grab inside of the contentSelector element. headingSelector: headerEl, //positionFixedSelector: '.toc', //positionFixedClass: 'is-position-fixed', scrollSmooth: true, scrollSmoothOffset: -80, headingsOffset: -500 }); }