Update toc.js
This commit is contained in:
@@ -13,6 +13,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
var stickyTop = 100;
|
var stickyTop = 100;
|
||||||
var headingOffset = 88;
|
var headingOffset = 88;
|
||||||
var ticking = false;
|
var ticking = false;
|
||||||
|
var isInitialized = false; // 👈 NEW
|
||||||
|
|
||||||
function isDesktop() {
|
function isDesktop() {
|
||||||
return desktopQuery.matches;
|
return desktopQuery.matches;
|
||||||
@@ -44,10 +45,13 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
|
|
||||||
var titleRect = title.getBoundingClientRect();
|
var titleRect = title.getBoundingClientRect();
|
||||||
var scrollTop = window.scrollY || window.pageYOffset || 0;
|
var scrollTop = window.scrollY || window.pageYOffset || 0;
|
||||||
|
|
||||||
var contentColumn = document.querySelector('.inside-page .main-wrapper > *:first-child, .inside-page .main-wrapper .primary-content, .inside-page .main-wrapper #primary, .inside-page .main-wrapper main');
|
var contentColumn = document.querySelector('.inside-page .main-wrapper > *:first-child, .inside-page .main-wrapper .primary-content, .inside-page .main-wrapper #primary, .inside-page .main-wrapper main');
|
||||||
var sidebar = document.querySelector('.inside-page .main-wrapper > aside, .inside-page .main-wrapper .widget-area, .inside-page .main-wrapper #secondary, .inside-page .main-wrapper .sidebar');
|
var sidebar = document.querySelector('.inside-page .main-wrapper > aside, .inside-page .main-wrapper .widget-area, .inside-page .main-wrapper #secondary, .inside-page .main-wrapper .sidebar');
|
||||||
|
|
||||||
var contentRect = contentColumn ? contentColumn.getBoundingClientRect() : titleRect;
|
var contentRect = contentColumn ? contentColumn.getBoundingClientRect() : titleRect;
|
||||||
var sidebarRect = sidebar ? sidebar.getBoundingClientRect() : null;
|
var sidebarRect = sidebar ? sidebar.getBoundingClientRect() : null;
|
||||||
|
|
||||||
var mirroredGap = 56;
|
var mirroredGap = 56;
|
||||||
|
|
||||||
if (sidebarRect) {
|
if (sidebarRect) {
|
||||||
@@ -62,6 +66,12 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
document.documentElement.style.setProperty('--zeitfresser-toc-top', tocTop + 'px');
|
document.documentElement.style.setProperty('--zeitfresser-toc-top', tocTop + 'px');
|
||||||
document.documentElement.style.setProperty('--zeitfresser-toc-left', tocLeft + 'px');
|
document.documentElement.style.setProperty('--zeitfresser-toc-left', tocLeft + 'px');
|
||||||
document.documentElement.style.setProperty('--zeitfresser-toc-width', tocWidth + 'px');
|
document.documentElement.style.setProperty('--zeitfresser-toc-width', tocWidth + 'px');
|
||||||
|
|
||||||
|
// 👇 NEW: reveal after first correct positioning
|
||||||
|
if (!isInitialized) {
|
||||||
|
toc.classList.add('is-ready');
|
||||||
|
isInitialized = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function setActiveLink(id) {
|
function setActiveLink(id) {
|
||||||
@@ -78,11 +88,10 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function updateProgress() {
|
function updateProgress() {
|
||||||
if (!progressBar) {
|
if (!progressBar) return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var article = document.querySelector('.single-post .post-content article, .single-post .post-content, article.post, article');
|
var article = document.querySelector('.single-post .post-content article, .single-post .post-content, article.post, article');
|
||||||
|
|
||||||
if (!article) {
|
if (!article) {
|
||||||
progressBar.style.width = '0%';
|
progressBar.style.width = '0%';
|
||||||
return;
|
return;
|
||||||
@@ -97,10 +106,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
|
|
||||||
function updateActiveHeading() {
|
function updateActiveHeading() {
|
||||||
var headings = getHeadings();
|
var headings = getHeadings();
|
||||||
|
if (!headings.length) return;
|
||||||
if (!headings.length) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var currentId = headings[0].target.id;
|
var currentId = headings[0].target.id;
|
||||||
var triggerY = headingOffset + 24;
|
var triggerY = headingOffset + 24;
|
||||||
@@ -115,9 +121,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onViewportChange() {
|
function onViewportChange() {
|
||||||
if (ticking) {
|
if (ticking) return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ticking = true;
|
ticking = true;
|
||||||
|
|
||||||
@@ -132,10 +136,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
links.forEach(function (link) {
|
links.forEach(function (link) {
|
||||||
link.addEventListener('click', function (event) {
|
link.addEventListener('click', function (event) {
|
||||||
var target = getTarget(link);
|
var target = getTarget(link);
|
||||||
|
if (!target) return;
|
||||||
if (!target) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
@@ -150,14 +151,10 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
if (nav) {
|
if (nav) {
|
||||||
nav.addEventListener('wheel', function (event) {
|
nav.addEventListener('wheel', function (event) {
|
||||||
var canScroll = nav.scrollHeight > nav.clientHeight;
|
var canScroll = nav.scrollHeight > nav.clientHeight;
|
||||||
|
if (!canScroll) return;
|
||||||
if (!canScroll) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var atTop = nav.scrollTop <= 0;
|
var atTop = nav.scrollTop <= 0;
|
||||||
var atBottom = Math.ceil(nav.scrollTop + nav.clientHeight) >= nav.scrollHeight;
|
var atBottom = Math.ceil(nav.scrollTop + nav.clientHeight) >= nav.scrollHeight;
|
||||||
@@ -169,6 +166,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
}, { passive: false });
|
}, { passive: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initial run
|
||||||
syncPosition();
|
syncPosition();
|
||||||
updateProgress();
|
updateProgress();
|
||||||
updateActiveHeading();
|
updateActiveHeading();
|
||||||
|
|||||||
Reference in New Issue
Block a user