(function() {
if (window.fofScrollInstalled) return;
window.fofScrollInstalled = true;
const ANIMATION_DISTANCE = 3100;
let galleryFrame = null;
let galleryStart = null;
let ticking = false;
function getScroll() {
return document.scrollingElement
? document.scrollingElement.scrollTop
: window.scrollY;
}
function receiveGallery(event) {
if (!event.data ||
event.data.type !== "fof-gallery-ready") return;
const frames = document.querySelectorAll("iframe");
for (const frame of frames) {
if (frame.contentWindow === event.source) {
galleryFrame = frame;
// Save original position before making it sticky.
galleryStart =
frame.getBoundingClientRect().top + getScroll();
// Keep the gallery visible during scrolling.
frame.style.position = "sticky";
frame.style.top = "0px";
frame.style.zIndex = "999";
frame.style.display = "block";
frame.style.width = "1024px";
frame.style.maxWidth = "100%";
frame.style.height = "500px";
frame.style.border = "0";
updateScroll();
break;
}
}
}
function updateScroll() {
if (!galleryFrame || galleryStart === null) return;
const distance = getScroll() - galleryStart;
const progress = Math.max(
0,
Math.min(1, distance / ANIMATION_DISTANCE)
);
galleryFrame.contentWindow.postMessage({
type: "fof-gallery-progress",
progress: progress
}, "*");
}
function requestUpdate() {
if (ticking) return;
ticking = true;
requestAnimationFrame(function() {
updateScroll();
ticking = false;
});
}
window.addEventListener("message", receiveGallery);
window.addEventListener("scroll", requestUpdate, {
passive: true
});
window.addEventListener("resize", requestUpdate);
})();