Skip to content

Commit e5cf1f2

Browse files
committed
Merge pull request #56 from bootstraponline/scroll_to_closest_header
Scroll to closest header
2 parents 6bf197d + 2f617a2 commit e5cf1f2

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

source/javascripts/lib/jquery.tocify.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147

148148
var self = this;
149149

150+
self.tocifyWrapper = $('.tocify-wrapper');
150151
self.extendPageScroll = true;
151152

152153
// Internal array that keeps track of all TOC items (Helps to recognize if there are duplicate TOC item strings)
@@ -703,6 +704,20 @@
703704
// Highlights the corresponding list item
704705
elem.addClass(self.focusClass);
705706

707+
// Scroll to highlighted element's header
708+
var tocifyWrapper = self.tocifyWrapper;
709+
var scrollToElem = $(elem).closest('.tocify-header');
710+
711+
var elementOffset = scrollToElem.offset().top,
712+
wrapperOffset = tocifyWrapper.offset().top;
713+
var offset = elementOffset - wrapperOffset;
714+
715+
if (offset >= $(window).height()) {
716+
var scrollPosition = offset + tocifyWrapper.scrollTop();
717+
tocifyWrapper.scrollTop(scrollPosition);
718+
} else if (offset < 0) {
719+
tocifyWrapper.scrollTop(0);
720+
}
706721
}
707722

708723
if(self.options.scrollHistory) {
@@ -1021,4 +1036,4 @@
10211036

10221037
});
10231038

1024-
})); //end of plugin
1039+
})); //end of plugin

0 commit comments

Comments
 (0)