pages/assets/js/common.js
Julian Wiest 729f136fb0
Add offset option to scrollspy initialization (#3354)
## Fix ToC Scroll-Spy Highlighting Issue

### Problem
When clicking on a ToC link in the sidebar, the page scrolls to the
correct section, but the ToC highlights the *previous* section instead
of the current one. The correct section only gets highlighted after
scrolling down a few pixels.

### Root Cause
The `scrollspy` in `assets/js/common.js` was initialized without an
offset parameter, so it didn't account for the fixed navigation header
height (~56px).

### Solution
Added `offset: 100` parameter to the scrollspy initialization to
properly detect when a section becomes visible, accounting for the
header and some buffer space.

### Changes
- Modified `assets/js/common.js`: Added offset parameter to scrollspy
configuration

### Testing
- [x] ToC correctly highlights the current section immediately after
clicking
- [x] Headings remain visible (not hidden under header)
- [x] Smooth scrolling works as expected

---------

Co-authored-by: Maruan <alshedivat@users.noreply.github.com>
2025-11-07 12:15:08 -08:00

61 lines
2.0 KiB
JavaScript

$(document).ready(function () {
// add toggle functionality to abstract, award and bibtex buttons
$("a.abstract").click(function () {
$(this).parent().parent().find(".abstract.hidden").toggleClass("open");
$(this).parent().parent().find(".award.hidden.open").toggleClass("open");
$(this).parent().parent().find(".bibtex.hidden.open").toggleClass("open");
});
$("a.award").click(function () {
$(this).parent().parent().find(".abstract.hidden.open").toggleClass("open");
$(this).parent().parent().find(".award.hidden").toggleClass("open");
$(this).parent().parent().find(".bibtex.hidden.open").toggleClass("open");
});
$("a.bibtex").click(function () {
$(this).parent().parent().find(".abstract.hidden.open").toggleClass("open");
$(this).parent().parent().find(".award.hidden.open").toggleClass("open");
$(this).parent().parent().find(".bibtex.hidden").toggleClass("open");
});
$("a").removeClass("waves-effect waves-light");
// bootstrap-toc
if ($("#toc-sidebar").length) {
// remove related publications years from the TOC
$(".publications h2").each(function () {
$(this).attr("data-toc-skip", "");
});
var navSelector = "#toc-sidebar";
var $myNav = $(navSelector);
Toc.init($myNav);
$("body").scrollspy({
target: navSelector,
offset: 100,
});
}
// add css to jupyter notebooks
const cssLink = document.createElement("link");
cssLink.href = "../css/jupyter.css";
cssLink.rel = "stylesheet";
cssLink.type = "text/css";
let jupyterTheme = determineComputedTheme();
$(".jupyter-notebook-iframe-container iframe").each(function () {
$(this).contents().find("head").append(cssLink);
if (jupyterTheme == "dark") {
$(this).bind("load", function () {
$(this).contents().find("body").attr({
"data-jp-theme-light": "false",
"data-jp-theme-name": "JupyterLab Dark",
});
});
}
});
// trigger popovers
$('[data-toggle="popover"]').popover({
trigger: "hover",
});
});