Skip to content

Commit

Permalink
(experiment) add script for offset to pubhist page
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemire60 committed Aug 15, 2024
1 parent fa0ab4e commit e9d61dd
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions pubhist.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,41 @@
permalink: /pubhist.html
---

<script>

// experiment to compensate for page header when jumping to anchors
// code from https://www.geeksforgeeks.org/offsetting-an-anchor-to-adjust-for-fixed-header/?ref=gcse_ind
// this is their option 2 for address this visual glitch

// Get the header element
let header = document.querySelector('header');

// Get the height of the header
let headerHeight = header.offsetHeight;
document.querySelectorAll('a[href^="#"]')
.forEach(function (anchor) {
anchor.addEventListener('click',
function (event) {
event.preventDefault();

// Get the target element that
// the anchor link points to
let target = document.querySelector(
this.getAttribute('href')
);

let targetPosition = target
.getBoundingClientRect().top - headerHeight;

window.scrollTo({
top: targetPosition + window.pageYOffset,
behavior: 'smooth'
});
});
});
</script>


<main id="main">
<section>
<div class="container" id="introduction">
Expand Down

0 comments on commit e9d61dd

Please sign in to comment.