解决 TOC 点击后的定位问题

This commit is contained in:
1152958806@qq.com 2023-10-13 22:02:41 +08:00
parent 339185e446
commit a16f1c6eea
1 changed files with 9 additions and 6 deletions

View File

@ -152,13 +152,12 @@ document.addEventListener('DOMContentLoaded', function () {
*/ */
const tocFn = function () { const tocFn = function () {
const postContent = document.querySelector('.post-content'); const postContent = document.querySelector('.post-content');
if (postContent == null) return; if (postContent == null) return;
const titles = postContent.querySelectorAll('h1,h2,h3,h4,h5,h6');
const headers = postContent.querySelectorAll('h1,h2,h3,h4,h5,h6');
// 没有 toc 目录,则直接移除 // 没有 toc 目录,则直接移除
if (headers.length === 0) { if (titles.length === 0 || !titles) {
document.getElementById("card-toc").remove(); const cardToc = document.getElementById("card-toc");
cardToc?.remove();
const $mobileTocButton = document.getElementById("mobile-toc-button") const $mobileTocButton = document.getElementById("mobile-toc-button")
if($mobileTocButton){ if($mobileTocButton){
$('#mobile-toc-button').attr('style', 'display: none'); $('#mobile-toc-button').attr('style', 'display: none');
@ -168,7 +167,11 @@ document.addEventListener('DOMContentLoaded', function () {
tocSelector: '.toc-content', tocSelector: '.toc-content',
contentSelector: '.post-content', contentSelector: '.post-content',
headingSelector: 'h1,h2,h3,h4,h5,h6', headingSelector: 'h1,h2,h3,h4,h5,h6',
hasInnerContainers: true collapseDepth: 6,
headingsOffset: 70,
scrollSmooth: true,
scrollSmoothOffset: -70,
tocScrollOffset: 50
}); });
} }