-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12222 from hasezoey/boldNav
docs: highlight current top-most visible header in navbar
- Loading branch information
Showing
3 changed files
with
42 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// this script changes the nav-bar entry that is current visible on the screen as a header (most to the top) to be bold | ||
|
||
let headers = undefined; | ||
let curNavEntry = undefined; | ||
let curElem = undefined; | ||
window.addEventListener('scroll', function() { | ||
// set these values if undefined, because otherwise in global scope they will not be defined because the window did not finish loading | ||
if (headers === undefined) { | ||
headers = document.querySelectorAll('.api-content > h3'); | ||
} | ||
if (curNavEntry === undefined) { | ||
const entries = document.querySelectorAll('.api-nav-content .nav-item .nav-item-sub'); | ||
for (const entry of entries) { | ||
if (window.getComputedStyle(entry).visibility !== "hidden") { | ||
curNavEntry = entry.parentElement; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
const scrollY = window.scrollY; | ||
let highlight = headers[0]; | ||
for (const header of headers) { | ||
if (header.offsetTop > scrollY) { | ||
break; | ||
} | ||
highlight = header; | ||
} | ||
if (curElem == undefined || highlight.id !== curElem.id) { | ||
// reset old element before re-assign | ||
if (curElem !== undefined) { | ||
document.querySelector('#' + curNavEntry.id + ' a[href="#' + curElem.id + '"]').style.fontWeight = "inherit"; | ||
} | ||
|
||
curElem = highlight; | ||
|
||
// add bold and visible to current ones | ||
document.querySelector('#' + curNavEntry.id + ' a[href="#' + curElem.id + '"]').style.fontWeight = 'bold'; | ||
} | ||
}); |