website/static/src/js/content.js

36 lines
1.1 KiB
JavaScript
Raw Normal View History

require("lite-youtube-embed");
2022-09-02 17:18:40 +01:00
const GLightbox = require("glightbox");
const clamp = require("lodash.clamp");
2022-09-02 17:18:40 +01:00
2023-10-15 22:29:40 +01:00
const SCROLL_INDICATOR = document.getElementById("scroll-indicator");
const CONTENT = document.querySelector(".container.content");
function handleScrollIndicator() {
// How far down the page does the content start?
const initialScroll = CONTENT.getBoundingClientRect().top + window.scrollY;
const contentHeight = CONTENT.getBoundingClientRect().height;
// How far down the page do we consider the content "read"?
const scrollTarget = window.innerHeight * 0.75;
const scrolled =
(window.scrollY - initialScroll + scrollTarget) / contentHeight;
const scrolledPercentage = clamp(scrolled * 100, 0, 100);
SCROLL_INDICATOR.style.width = `${scrolledPercentage.toFixed(2)}%`;
}
2022-09-02 17:18:40 +01:00
window.addEventListener("load", () => {
if (CONTENT && SCROLL_INDICATOR) {
window.addEventListener("resize", handleScrollIndicator);
window.addEventListener("scroll", handleScrollIndicator);
// Initialize the indicator
handleScrollIndicator();
}
2023-10-15 22:29:40 +01:00
GLightbox({});
2023-10-15 22:29:40 +01:00
});