|
613 | 613 | // The zero timeout ensures the following code is run after the scroll events |
614 | 614 | setTimeout(function() { |
615 | 615 |
|
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_ |
618 | 617 |
|
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, |
621 | 620 |
|
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, |
624 | 623 |
|
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]"); |
627 | 626 |
|
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 | + }) |
630 | 637 |
|
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") + '"]'); |
633 | 640 |
|
634 | | - } |
| 641 | + // If the `highlightOnScroll` option is true and a next element is found |
| 642 | + if(self.options.highlightOnScroll && elem.length) { |
635 | 643 |
|
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); |
638 | 646 |
|
639 | | - self._triggerShow(elem, true); |
| 647 | + // Highlights the corresponding list item |
| 648 | + elem.addClass(self.focusClass); |
| 649 | + } |
640 | 650 |
|
641 | | - } |
| 651 | + // If the `showAndHideOnScroll` option is true |
| 652 | + if(self.options.showAndHideOnScroll && self.options.showAndHide) { |
642 | 653 |
|
643 | | - return false; |
| 654 | + self._triggerShow(elem, true); |
644 | 655 |
|
645 | | - } |
| 656 | + } |
646 | 657 |
|
647 | | - }); |
648 | 658 |
|
649 | 659 | }, 0); |
650 | 660 |
|
|
0 commit comments