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

Adding new option #42

Open
wants to merge 4 commits into
base: master
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
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Defaults shown below
'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
'addBottomPadding': false, //can receive a selector to which it will add padding in case the navigated element is out of view
'anchorName': function(i, heading, prefix) { //custom function for anchor name
return prefix+i;
},
Expand Down
43 changes: 43 additions & 0 deletions lib/toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,49 @@ var verboseIdCache = {};
$.fn.toc = function(options) {
var self = this;
var opts = $.extend({}, jQuery.fn.toc.defaults, options);
var $doc = $(document);
var $window = $(window);
var $bottomPaddingElement = opts.addBottomPadding ? $(opts.addBottomPadding) : [];

var container = $(opts.container);
var headings = $(opts.selectors, container);
var headingOffsets = [];
var activeClassName = opts.activeClass;

var addPaddingToElement = function (element) {
var needsPadding,
currentPadding = parseInt($bottomPaddingElement.css('paddingBottom').replace('px',''), 10) || 0,
padding = '',
docHeight = $doc.height(),
winHeight = $window.height();

needsPadding = (winHeight + element.offset().top) > (docHeight - currentPadding);

var updatePadding = function () {
$bottomPaddingElement.css({
paddingBottom : padding
});
};

if (needsPadding) {
padding = (winHeight + element.offset().top) - docHeight + currentPadding + 25;

if (padding !== currentPadding){
if (padding < currentPadding){
// We wait till the scrolling happened to remove the extra padding
var speed = typeof opts.smoothScrolling === 'function' ? 425 : 10;
setTimeout(updatePadding, speed);
}
else {
updatePadding();
}
}
}
else if (currentPadding){
updatePadding();
}
};

var scrollTo = function(e, callback) {
if (opts.smoothScrolling && typeof opts.smoothScrolling === 'function') {
e.preventDefault();
Expand Down Expand Up @@ -69,6 +106,11 @@ $.fn.toc = function(options) {
.text(opts.headerText(i, heading, $h))
.attr('href', '#' + anchorName)
.bind('click', function(e) {
if ($bottomPaddingElement.length){

addPaddingToElement($($(e.target).attr('href')));
}

$(window).unbind('scroll', highlightOnScroll);
scrollTo(e, function() {
$(window).bind('scroll', highlightOnScroll);
Expand Down Expand Up @@ -104,6 +146,7 @@ jQuery.fn.toc.defaults = {
onHighlight: function() {},
highlightOnScroll: true,
highlightOffset: 100,
addBottomPadding: false,
anchorName: function(i, heading, prefix) {
if(heading.id.length) {
return heading.id;
Expand Down
15 changes: 15 additions & 0 deletions test/toc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ suite('toc', function() {
}, 410);
});

// This test does pass on the browser but PhantomJS do weird things to the body so conditions never match
test.skip('should add padding to a given element so it can be at the top', function (done) {
$('.toc').toc({
container: '#fixture',
addBottomPadding: '#second_wrapper'
});

$('.toc a:last').click();

setTimeout(function () {
assert.ok($('#second_wrapper').attr('style').indexOf('padding-bottom') !== -1);
done();
}, 400);
});

test('should update on scroll', function(done) {
assert.equal($(window).scrollTop(), 0);

Expand Down