mirror of
https://github.com/yv1ing/MollyBlog.git
synced 2025-09-16 14:53:45 +08:00
new: Add directory progress tracking style
This commit is contained in:
@@ -130,6 +130,12 @@ tr, th, td {
|
|||||||
padding-left: 50px;
|
padding-left: 50px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.active a {
|
||||||
|
color: var(--primary-color);
|
||||||
|
transform: scale(1.2);
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
||||||
.image-preview-wrap {
|
.image-preview-wrap {
|
||||||
position: fixed;
|
position: fixed;
|
||||||
top: 0;
|
top: 0;
|
||||||
|
|||||||
@@ -1,31 +1,40 @@
|
|||||||
const tocBox = document.getElementById('toc-box');
|
const tocBox = document.getElementById('toc-box');
|
||||||
|
const postContent = document.getElementById('post-content');
|
||||||
const tList = document.getElementById('post-content').querySelectorAll('h1, h2, h3, h4, h5, h6');
|
const tList = postContent.querySelectorAll('h1, h2, h3, h4, h5, h6');
|
||||||
const hList = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6'];
|
const hList = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6'];
|
||||||
|
const div = document.createElement('div');
|
||||||
|
|
||||||
let tmpHtml = `<div>\n`;
|
tList.forEach(v => {
|
||||||
|
|
||||||
Array.from(tList, v => {
|
|
||||||
// get the level number
|
|
||||||
const H = hList.indexOf(v.nodeName) + 1 || 1;
|
const H = hList.indexOf(v.nodeName) + 1 || 1;
|
||||||
tmpHtml += `<div class="li li-${H} toc-li"><a id="${v.id}" href="#">${v.textContent}</a></div>\n`;
|
const liDiv = document.createElement('div');
|
||||||
|
liDiv.className = `li li-${H} toc-li`;
|
||||||
|
const a = document.createElement('a');
|
||||||
|
a.id = v.id;
|
||||||
|
a.href = "#";
|
||||||
|
a.textContent = v.textContent;
|
||||||
|
liDiv.appendChild(a);
|
||||||
|
div.appendChild(liDiv);
|
||||||
|
a.addEventListener('click', (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
window.scrollTo({ top: v.offsetTop - 60, behavior: 'smooth' });
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tmpHtml += `</div>`
|
tocBox.appendChild(div);
|
||||||
tocBox.innerHTML = tmpHtml;
|
window.addEventListener('scroll', () => {
|
||||||
|
let currentActive = null;
|
||||||
Array.from(tList, v => {
|
tList.forEach(v => {
|
||||||
const btn = document.getElementById('toc-box').querySelector(`#${v.id}`);
|
if (window.scrollY >= v.offsetTop - 100) {
|
||||||
const ele = document.getElementById('post-content').querySelector(`#${v.id}`);
|
currentActive = v;
|
||||||
|
}
|
||||||
if (!btn || !ele) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
btn.addEventListener('click', (event) => {
|
|
||||||
event.preventDefault();
|
|
||||||
window.scrollTo({ top: ele.offsetTop - 60, behavior: 'smooth' });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (currentActive) {
|
||||||
|
tList.forEach(v => {
|
||||||
|
document.getElementById(v.id).parentNode.classList.remove('active');
|
||||||
|
});
|
||||||
|
document.getElementById(currentActive.id).parentNode.classList.add('active');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function backToTop() {
|
function backToTop() {
|
||||||
|
|||||||
Reference in New Issue
Block a user