PoC page/scroll-stability when collapsing

This commit is contained in:
Klaas van Schelven
2023-11-17 18:56:01 +01:00
parent 73764a11d4
commit ab72931bf0
2 changed files with 31 additions and 10 deletions

View File

@@ -1,15 +1,35 @@
"use strict";
function distanceToWindowBottom() {
// https://stackoverflow.com/a/2800676/339144
let scrollPosition = window.pageYOffset;
let windowSize = window.innerHeight;
let bodyHeight = document.body.offsetHeight;
return Math.max(bodyHeight - (scrollPosition + windowSize), 0);
}
onscroll = (event) => {
console.log(distanceToWindowBottom());
};
// This is the important part!
function collapseSection(element) {
if (element.getAttribute("data-collapsed") === "true") {
return;
}
element.classList.remove("xl:flex"); // this appears to be necessary, not sure why
// get the height of the element's inner content, regardless of its actual size
var sectionHeight = element.scrollHeight;
let bodyHeightPreCollapse = document.body.offsetHeight;
console.log("?", sectionHeight, distanceToWindowBottom());
if (sectionHeight > distanceToWindowBottom()) {
let body = document.querySelector("body");
console.log((bodyHeightPreCollapse - distanceToWindowBottom()) + "px");
body.style.minHeight = (bodyHeightPreCollapse - distanceToWindowBottom()) + "px";
}
element.classList.remove("xl:flex"); // this appears to be necessary, not sure why
// temporarily disable all css transitions
var elementTransition = element.style.transition;