-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable sticky aside for sidebars in forum
- Loading branch information
Showing
6 changed files
with
38 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const stickyAside = document.querySelector(".sticky-aside"); | ||
const navbar = document.querySelector("nav.navbar"); | ||
const header = document.querySelector("header"); | ||
|
||
function reposition() { | ||
const asideHeight = stickyAside.getBoundingClientRect().height; | ||
const viewportHeight = window.innerHeight; | ||
let navbarHeight = getNavbarHeight(); | ||
if (navbarHeight + asideHeight < viewportHeight) { | ||
stickyAside.style.top = navbarHeight + "px"; // pin top top | ||
} else { | ||
stickyAside.style.top = viewportHeight - asideHeight + "px"; // pin to bottom | ||
} | ||
} | ||
|
||
function getNavbarHeight() { | ||
if (header.classList.contains("fixed-top")) { | ||
// if navbar is fixed, then `.bottom` will always be bigger | ||
// than 0, but we add Math.max() in case styles are changed | ||
// or something gets renamed | ||
return Math.max(navbar.getBoundingClientRect().bottom, 0); | ||
} | ||
return 0; | ||
} | ||
|
||
window.addEventListener("resize", reposition, {passive: true}); | ||
reposition(); | ||
[0, 1000, 4000, 6000, 10000].forEach(time => setTimeout(reposition, time)); |
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,6 @@ | ||
.sticky-aside { | ||
position: sticky; | ||
padding: 15px 0; | ||
margin: -15px 0; | ||
transition: top .15s; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<aside id="sidebar"> | ||
<aside class="sticky-aside" id="sidebar"> | ||
{% block side_menu %} | ||
{% endblock %} | ||
|
||
|
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