Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Back to top feature #69

Open
wants to merge 2 commits into
base: greenkeeper/update-all
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ jQuery.fn.toc.defaults = {
if (verboseIdCache[candidateId]) {
var j = 2;

while(verboseIdCache[candidateId + j]) {
while(verboseIdCache[candidateId + '-' + j]) {
j++;
}
candidateId = candidateId + '-' + j;
Expand Down
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Defaults shown below
'listType': '<ul/>', //use unordered list. If you need ordered one instead pass: '<ol/>'
'smoothScrolling': true, //enable or disable smooth scrolling on click
'prefix': 'toc', //prefix for anchor tags and class names
'backToTop': false, //add back to top link on heading
'backToTopLabel': 'Back to top', //label used for back to top link
'onHighlight': function(el) {}, //called when a new section is highlighted
'highlightOnScroll': true, //add class to heading that is currently in focus
'highlightOffset': 100, //offset to trigger the next headline
Expand Down
22 changes: 22 additions & 0 deletions lib/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,28 @@ $.fn.toc = function(options) {
headingOffsets.push($h.offset().top - opts.highlightOffset);

var anchorName = opts.anchorName(i, heading, opts.prefix);
var anchorNameToTop = anchorName + '-to-top';

//add anchor
if(heading.id !== anchorName) {
var anchor = $('<span/>').attr('id', anchorName).insertBefore($h);
}

// Add back to top link.
if (opts.backToTop) {
var anchorToTop = $('<a/>')
.attr('href', '#' + anchorNameToTop)
.text(opts.backToTopLabel)
.insertAfter($h)
.bind('click', function(e) {
$(window).unbind('scroll', highlightOnScroll);
scrollTo(e, function() {
$(window).bind('scroll', highlightOnScroll);
});
el.trigger('selected', $(this).attr('href'));
});
}

//build TOC item
var a = $('<a/>')
.text(opts.headerText(i, heading, $h))
Expand All @@ -76,6 +92,10 @@ $.fn.toc = function(options) {
el.trigger('selected', $(this).attr('href'));
});

if (opts.backToTop) {
a.attr('id', anchorNameToTop);
}

var li = $('<li/>')
.addClass(opts.itemClass(i, heading, $h, opts.prefix))
.append(a);
Expand All @@ -100,6 +120,8 @@ jQuery.fn.toc.defaults = {
},
scrollToOffset: 0,
prefix: 'toc',
backToTop: false,
backToTopLabel: 'Back to top',
activeClass: 'toc-active',
onHighlight: function() {},
highlightOnScroll: true,
Expand Down