Skip to content

Commit 449a048

Browse files
committed
Merge pull request #35 from andrekampert/master
Highlight closest element rather than first element within the highlight offset
2 parents 2892212 + 614729a commit 449a048

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

src/javascripts/jquery.tocify.js

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -613,38 +613,48 @@
613613
// The zero timeout ensures the following code is run after the scroll events
614614
setTimeout(function() {
615615

616-
// Loops through each anchor tag on the page with a `name` attribute
617-
$(self.options.context).find("div[data-unique]").next().each(function() {
616+
// _Local variables_
618617

619-
// If the user has scrolled to within x (the highlightOffset option) distance of the currently traversed anchor tag
620-
if ((Math.abs($(this).offset().top - winScrollTop) < self.options.highlightOffset)) {
618+
// Stores the distance to the closest anchor
619+
var closestAnchorDistance = null,
621620

622-
// Stores the list item HTML element that corresponds to the currently traversed anchor tag
623-
elem = $('li[data-unique="' + $(this).prev("div[data-unique]").attr("data-unique") + '"]');
621+
// Stores the index of the closest anchor
622+
closestAnchorIdx = null,
624623

625-
// If the `highlightOnScroll` option is true and a next element is found
626-
if(self.options.highlightOnScroll && elem.length) {
624+
// Keeps a reference to all anchors
625+
anchors = $(self.options.context).find("div[data-unique]");
627626

628-
// Removes highlighting from all of the list item's
629-
self.element.find("." + self.focusClass).removeClass(self.focusClass);
627+
// Determines the index of the closest anchor
628+
anchors.each(function(idx) {
629+
var distance = Math.abs($(this).next().offset().top - winScrollTop - self.options.highlightOffset);
630+
if (closestAnchorDistance == null || distance < closestAnchorDistance) {
631+
closestAnchorDistance = distance;
632+
closestAnchorIdx = idx;
633+
} else {
634+
return false;
635+
}
636+
})
630637

631-
// Highlights the corresponding list item
632-
elem.addClass(self.focusClass);
638+
// Stores the list item HTML element that corresponds to the currently traversed anchor tag
639+
elem = $('li[data-unique="' + $(anchors[closestAnchorIdx]).attr("data-unique") + '"]');
633640

634-
}
641+
// If the `highlightOnScroll` option is true and a next element is found
642+
if(self.options.highlightOnScroll && elem.length) {
635643

636-
// If the `showAndHideOnScroll` option is true
637-
if(self.options.showAndHideOnScroll && self.options.showAndHide) {
644+
// Removes highlighting from all of the list item's
645+
self.element.find("." + self.focusClass).removeClass(self.focusClass);
638646

639-
self._triggerShow(elem, true);
647+
// Highlights the corresponding list item
648+
elem.addClass(self.focusClass);
649+
}
640650

641-
}
651+
// If the `showAndHideOnScroll` option is true
652+
if(self.options.showAndHideOnScroll && self.options.showAndHide) {
642653

643-
return false;
654+
self._triggerShow(elem, true);
644655

645-
}
656+
}
646657

647-
});
648658

649659
}, 0);
650660

src/javascripts/jquery.tocify.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)