|
572 | 572 | } |
573 | 573 | }); |
574 | 574 |
|
| 575 | + // only attach handler if needed (expensive in IE) |
| 576 | + if (self.options.extendPage || self.options.highlightOnScroll || self.options.scrollHistory || self.options.showAndHideOnScroll) |
| 577 | + { |
575 | 578 | // Window scroll event handler |
576 | | - $(window).on("scroll.tocify", function() { |
| 579 | + $(window).on("scroll.tocify", function() { |
577 | 580 |
|
578 | | - // Once all animations on the page are complete, this callback function will be called |
579 | | - $("html, body").promise().done(function() { |
| 581 | + // Once all animations on the page are complete, this callback function will be called |
| 582 | + $("html, body").promise().done(function() { |
| 583 | + |
| 584 | + // Local variables |
580 | 585 |
|
581 | | - // Local variables |
| 586 | + // Stores how far the user has scrolled |
| 587 | + var winScrollTop = $(window).scrollTop(), |
582 | 588 |
|
583 | | - // Stores how far the user has scrolled |
584 | | - var winScrollTop = $(window).scrollTop(), |
| 589 | + // Stores the height of the window |
| 590 | + winHeight = $(window).height(), |
585 | 591 |
|
586 | | - // Stores the height of the window |
587 | | - winHeight = $(window).height(), |
| 592 | + // Stores the height of the document |
| 593 | + docHeight = $(document).height(), |
588 | 594 |
|
589 | | - // Stores the height of the document |
590 | | - docHeight = $(document).height(), |
| 595 | + scrollHeight = $("body")[0].scrollHeight, |
591 | 596 |
|
592 | | - scrollHeight = $("body")[0].scrollHeight, |
| 597 | + // Instantiates a variable that will be used to hold a selected HTML element |
| 598 | + elem, |
593 | 599 |
|
594 | | - // Instantiates a variable that will be used to hold a selected HTML element |
595 | | - elem, |
| 600 | + lastElem, |
596 | 601 |
|
597 | | - lastElem, |
| 602 | + lastElemOffset, |
598 | 603 |
|
599 | | - lastElemOffset, |
| 604 | + currentElem; |
600 | 605 |
|
601 | | - currentElem; |
| 606 | + if(self.options.extendPage) { |
602 | 607 |
|
603 | | - if(self.options.extendPage) { |
| 608 | + // If the user has scrolled to the bottom of the page and the last toc item is not focused |
| 609 | + if((self.webkit && winScrollTop >= scrollHeight - winHeight - self.options.extendPageOffset) || (!self.webkit && winHeight + winScrollTop > docHeight - self.options.extendPageOffset)) { |
604 | 610 |
|
605 | | - // If the user has scrolled to the bottom of the page and the last toc item is not focused |
606 | | - if((self.webkit && winScrollTop >= scrollHeight - winHeight - self.options.extendPageOffset) || (!self.webkit && winHeight + winScrollTop > docHeight - self.options.extendPageOffset)) { |
| 611 | + if(!$(extendPageClass).length) { |
607 | 612 |
|
608 | | - if(!$(extendPageClass).length) { |
| 613 | + lastElem = $('div[data-unique="' + $(itemClass).last().attr("data-unique") + '"]'); |
609 | 614 |
|
610 | | - lastElem = $('div[data-unique="' + $(itemClass).last().attr("data-unique") + '"]'); |
| 615 | + if(!lastElem.length) return; |
611 | 616 |
|
612 | | - if(!lastElem.length) return; |
| 617 | + // Gets the top offset of the page header that is linked to the last toc item |
| 618 | + lastElemOffset = lastElem.offset().top; |
613 | 619 |
|
614 | | - // Gets the top offset of the page header that is linked to the last toc item |
615 | | - lastElemOffset = lastElem.offset().top; |
| 620 | + // Appends a div to the bottom of the page and sets the height to the difference of the window scrollTop and the last element's position top offset |
| 621 | + $(self.options.context).append($("<div />", { |
616 | 622 |
|
617 | | - // Appends a div to the bottom of the page and sets the height to the difference of the window scrollTop and the last element's position top offset |
618 | | - $(self.options.context).append($("<div />", { |
| 623 | + "class": extendPageClassName, |
619 | 624 |
|
620 | | - "class": extendPageClassName, |
| 625 | + "height": Math.abs(lastElemOffset - winScrollTop) + "px", |
621 | 626 |
|
622 | | - "height": Math.abs(lastElemOffset - winScrollTop) + "px", |
| 627 | + "data-unique": extendPageClassName |
623 | 628 |
|
624 | | - "data-unique": extendPageClassName |
| 629 | + })); |
625 | 630 |
|
626 | | - })); |
| 631 | + if(self.extendPageScroll) { |
627 | 632 |
|
628 | | - if(self.extendPageScroll) { |
| 633 | + currentElem = self.element.find('li.active'); |
629 | 634 |
|
630 | | - currentElem = self.element.find('li.active'); |
| 635 | + self._scrollTo($("div[data-unique=" + currentElem.attr("data-unique") + "]")); |
631 | 636 |
|
632 | | - self._scrollTo($("div[data-unique=" + currentElem.attr("data-unique") + "]")); |
| 637 | + } |
633 | 638 |
|
634 | 639 | } |
635 | 640 |
|
636 | 641 | } |
637 | 642 |
|
638 | 643 | } |
639 | 644 |
|
640 | | - } |
641 | | - |
642 | | - // The zero timeout ensures the following code is run after the scroll events |
643 | | - setTimeout(function() { |
| 645 | + // The zero timeout ensures the following code is run after the scroll events |
| 646 | + setTimeout(function() { |
644 | 647 |
|
645 | | - // _Local variables_ |
| 648 | + // _Local variables_ |
646 | 649 |
|
647 | | - // Stores the distance to the closest anchor |
648 | | - var closestAnchorDistance = null, |
| 650 | + // Stores the distance to the closest anchor |
| 651 | + var closestAnchorDistance = null, |
649 | 652 |
|
650 | | - // Stores the index of the closest anchor |
651 | | - closestAnchorIdx = null, |
| 653 | + // Stores the index of the closest anchor |
| 654 | + closestAnchorIdx = null, |
652 | 655 |
|
653 | | - // Keeps a reference to all anchors |
654 | | - anchors = $(self.options.context).find("div[data-unique]"), |
| 656 | + // Keeps a reference to all anchors |
| 657 | + anchors = $(self.options.context).find("div[data-unique]"), |
655 | 658 |
|
656 | | - anchorText; |
| 659 | + anchorText; |
657 | 660 |
|
658 | | - // Determines the index of the closest anchor |
659 | | - anchors.each(function(idx) { |
660 | | - var distance = Math.abs(($(this).next().length ? $(this).next() : $(this)).offset().top - winScrollTop - self.options.highlightOffset); |
661 | | - if (closestAnchorDistance == null || distance < closestAnchorDistance) { |
662 | | - closestAnchorDistance = distance; |
663 | | - closestAnchorIdx = idx; |
664 | | - } else { |
665 | | - return false; |
666 | | - } |
667 | | - }); |
| 661 | + // Determines the index of the closest anchor |
| 662 | + anchors.each(function(idx) { |
| 663 | + var distance = Math.abs(($(this).next().length ? $(this).next() : $(this)).offset().top - winScrollTop - self.options.highlightOffset); |
| 664 | + if (closestAnchorDistance == null || distance < closestAnchorDistance) { |
| 665 | + closestAnchorDistance = distance; |
| 666 | + closestAnchorIdx = idx; |
| 667 | + } else { |
| 668 | + return false; |
| 669 | + } |
| 670 | + }); |
668 | 671 |
|
669 | | - anchorText = $(anchors[closestAnchorIdx]).attr("data-unique"); |
| 672 | + anchorText = $(anchors[closestAnchorIdx]).attr("data-unique"); |
670 | 673 |
|
671 | | - // Stores the list item HTML element that corresponds to the currently traversed anchor tag |
672 | | - elem = $('li[data-unique="' + anchorText + '"]'); |
| 674 | + // Stores the list item HTML element that corresponds to the currently traversed anchor tag |
| 675 | + elem = $('li[data-unique="' + anchorText + '"]'); |
673 | 676 |
|
674 | | - // If the `highlightOnScroll` option is true and a next element is found |
675 | | - if(self.options.highlightOnScroll && elem.length) { |
| 677 | + // If the `highlightOnScroll` option is true and a next element is found |
| 678 | + if(self.options.highlightOnScroll && elem.length) { |
676 | 679 |
|
677 | | - // Removes highlighting from all of the list item's |
678 | | - self.element.find("." + self.focusClass).removeClass(self.focusClass); |
| 680 | + // Removes highlighting from all of the list item's |
| 681 | + self.element.find("." + self.focusClass).removeClass(self.focusClass); |
679 | 682 |
|
680 | | - // Highlights the corresponding list item |
681 | | - elem.addClass(self.focusClass); |
| 683 | + // Highlights the corresponding list item |
| 684 | + elem.addClass(self.focusClass); |
682 | 685 |
|
683 | | - } |
| 686 | + } |
684 | 687 |
|
685 | | - if(self.options.scrollHistory) { |
| 688 | + if(self.options.scrollHistory) { |
686 | 689 |
|
687 | | - if(window.location.hash !== "#" + anchorText) { |
| 690 | + if(window.location.hash !== "#" + anchorText) { |
688 | 691 |
|
689 | | - window.location.replace("#" + anchorText); |
| 692 | + window.location.replace("#" + anchorText); |
690 | 693 |
|
| 694 | + } |
691 | 695 | } |
692 | | - } |
693 | 696 |
|
694 | | - // If the `showAndHideOnScroll` option is true |
695 | | - if(self.options.showAndHideOnScroll && self.options.showAndHide) { |
| 697 | + // If the `showAndHideOnScroll` option is true |
| 698 | + if(self.options.showAndHideOnScroll && self.options.showAndHide) { |
696 | 699 |
|
697 | | - self._triggerShow(elem, true); |
| 700 | + self._triggerShow(elem, true); |
698 | 701 |
|
699 | | - } |
| 702 | + } |
700 | 703 |
|
701 | | - }, 0); |
| 704 | + }, 0); |
702 | 705 |
|
703 | | - }); |
| 706 | + }); |
704 | 707 |
|
705 | | - }); |
| 708 | + }); |
| 709 | + } |
706 | 710 |
|
707 | 711 | }, |
708 | 712 |
|
|
0 commit comments