Fixed the anchor offset issue.

This commit is contained in:
Bob Vandevliet 2024-07-29 17:17:52 +02:00
parent 35f1c3a9e8
commit 4c68d18b10
2 changed files with 15 additions and 11 deletions

View file

@ -28,17 +28,20 @@ document.addEventListener('DOMContentLoaded', () =>
/**
* @type {NodeListOf<HTMLElement>}
*/
const sections = document.querySelectorAll('.section');
const sections = document.querySelectorAll('section');
sections.forEach(section =>
{
if (section.id)
const anchorTarget = section.querySelector('[id^="section-"]');
const anchorHeader = section.querySelector('h1,h2');
if (anchorTarget && anchorHeader)
{
const anchor = document.createElement('a');
anchor.className = 'anchor-link';
anchor.href = `#${section.id}`;
anchor.innerHTML = ' #&nbsp;';
section.appendChild(anchor);
const anchorLink = document.createElement('a');
anchorLink.className = 'anchor-link';
anchorLink.href = `#${anchorTarget.id}`;
anchorLink.innerHTML = ' #&nbsp;';
anchorHeader.appendChild(anchorLink);
}
});