-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for sticky banners and utilize them (#2992)
- Loading branch information
Showing
4 changed files
with
41 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"use strict"; | ||
|
||
// Inject a style element into the document head that adds scroll-margin-top to | ||
// all elements with an id attribute. This is used to offset the scroll position | ||
// when clicking on a link to an element with an id attribute. The offset is | ||
// equal to the height of the sticky banner. | ||
document.addEventListener("DOMContentLoaded", () => { | ||
const stickyBanners = document.getElementsByClassName("sticky-banner"); | ||
if (!stickyBanners.length) { | ||
return; | ||
} | ||
|
||
const stickyBanner = stickyBanners[0]; | ||
const node = document.createElement("style"); | ||
node.id = "sticky-banner-style"; | ||
document.head.appendChild(node); | ||
|
||
function adjustBannerMargin() { | ||
const text = document.createTextNode( | ||
":target { scroll-margin-top: " + stickyBanner.offsetHeight + "px; }" | ||
); | ||
node.replaceChildren(text); | ||
} | ||
|
||
adjustBannerMargin(); | ||
document.addEventListener("resize", adjustBannerMargin); | ||
document.addEventListener("load", adjustBannerMargin); | ||
}); |
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