fix: toc no content bug

This commit is contained in:
XPoet 2020-05-06 21:56:18 +08:00
parent 82b2ba7ebd
commit 8fca3708d9
1 changed files with 7 additions and 6 deletions

View File

@ -1,8 +1,8 @@
window.addEventListener('DOMContentLoaded', () => { window.addEventListener('DOMContentLoaded', () => {
const tocElement = document.querySelector('.post-toc-wrap'); const articleToc = document.querySelector('.article-toc');
const postTocWrap = document.querySelector('.post-toc-wrap');
const navItems = tocElement.querySelectorAll('.post-toc li'); const navItems = postTocWrap.querySelectorAll('.post-toc li');
if (navItems.length > 0) { if (navItems.length > 0) {
@ -38,10 +38,10 @@ window.addEventListener('DOMContentLoaded', () => {
} }
// Scrolling to center active TOC element if TOC content is taller then viewport. // Scrolling to center active TOC element if TOC content is taller then viewport.
window.anime({ window.anime({
targets: tocElement, targets: postTocWrap,
duration: 200, duration: 200,
easing: 'linear', easing: 'linear',
scrollTop: tocElement.scrollTop - (tocElement.offsetHeight / 2) + target.getBoundingClientRect().top - tocElement.getBoundingClientRect().top scrollTop: postTocWrap.scrollTop - (postTocWrap.offsetHeight / 2) + target.getBoundingClientRect().top - postTocWrap.getBoundingClientRect().top
}); });
} }
@ -86,7 +86,8 @@ window.addEventListener('DOMContentLoaded', () => {
createIntersectionObserver(document.documentElement.scrollHeight); createIntersectionObserver(document.documentElement.scrollHeight);
} else { } else {
tocElement.innerHTML = ''; postTocWrap.innerHTML = '';
articleToc.style.display = 'none';
} }