From a314dc039d6495d0b07b711cd49a4ae67f339c9c Mon Sep 17 00:00:00 2001 From: liuyib <1656081615@qq.com> Date: Fri, 13 Mar 2020 12:51:20 +0800 Subject: [PATCH] fix: Toc highlighting is incorrect --- source/js/sidebar.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/source/js/sidebar.js b/source/js/sidebar.js index 478b6e0..af4bda7 100644 --- a/source/js/sidebar.js +++ b/source/js/sidebar.js @@ -18,7 +18,7 @@ $(document).ready(function () { var currHeading = null; // The heading that reached the top last time. var lastHeading = null; - var isRemoveTocClass = false; + var isRemovedTocClass = false; // Automatically expand items in the article directory // based on the scrolling of heading in the article. @@ -43,25 +43,22 @@ $(document).ready(function () { if ( $postBody[0] && $firsetChild[0] && + $firsetChild[0].getBoundingClientRect().top > 0 && $firsetChild.offset().top - $(window).scrollTop() > 0 ) { - if (!isRemoveTocClass) { + if (!isRemovedTocClass) { $allTocItem.removeClass('active current'); - isRemoveTocClass = true; + isRemovedTocClass = true; } return; } - if (currHeading !== lastHeading) { var $targetLink = $('.sidebar-toc a[href="#' + currHeading + '"]'); - // If the relevant "" is not found, remain the state of the toc, - // either, remove styles for all active states. - if ($targetLink[0]) { - $allTocItem.removeClass('active current'); - } + $allTocItem.removeClass('active current'); $targetLink.parents('li').addClass('active'); $targetLink.parent().addClass('current'); lastHeading = currHeading; + isRemovedTocClass = false; } } -- GitLab