Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解决加密文章寻找不到Toc列表错误 #370

Open
wants to merge 1 commit into
base: card
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions source/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,18 @@
headerH = header.clientHeight,
titles = $('#post-content').querySelectorAll('h1, h2, h3, h4, h5, h6');

toc.querySelector('a[href="#' + titles[0].id + '"]').parentNode.classList.add('active');
var firstLink = titles.length > 0 ? toc.querySelector('a[href="#' + titles[0].id + '"]') : toc.querySelectorAll('a')[0];

if (firstLink)
firstLink.parentNode.classList.add('active');

// Make every child shrink initially
var tocChilds = toc.querySelectorAll('.post-toc-child');
for (i = 0, len = tocChilds.length; i < len; i++) {
tocChilds[i].classList.add('post-toc-shrink');
}
var firstChild =
toc.querySelector('a[href="#' + titles[0].id + '"]')
.nextElementSibling;
firstLink.nextElementSibling;
if (firstChild) {
firstChild.classList.add('post-toc-expand');
firstChild.classList.remove('post-toc-shrink');
Expand Down Expand Up @@ -152,6 +154,11 @@
top >= bannerH - headerH ? toc.classList.add('fixed') : toc.classList.remove('fixed');
},
actived: function (top) {

if (titles.length == 0) {
titles = $('#post-content').querySelectorAll('h1, h2, h3, h4, h5, h6');
}

for (i = 0, len = titles.length; i < len; i++) {
if (top > offset(titles[i]).y - headerH - 5) {
var prevListEle = toc.querySelector('li.active');
Expand All @@ -161,7 +168,7 @@
}
}

if (top < offset(titles[0]).y) {
if (titles.length > 0 && top < offset(titles[0]).y) {
handleTocActive(
toc.querySelector('li.active'),
toc.querySelector('a[href="#' + titles[0].id + '"]').parentNode
Expand Down