Published
Edited
Oct 8, 2021
Importers
24 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const sidebar = html`<div class=sidebar>
<p><strong>A sticky sidebar</strong></p>
<p style="max-width:200px;padding-top:3em">It has some content to stretch its height.`;
const container = html`<div class=container style="height:1500px">${sidebar}`;
const unstick = makeSticky(container, sidebar, options);
invalidation.then(unstick);
return container;
}
Insert cell
Insert cell
Insert cell
function makeSticky(context, element, {transition = 75, offset = 0, ease = 'ease-in'} = {}) {
element.style.position = 'absolute';
element.style.transition = `top ${transition}ms ${ease}`;
return observeViewport((top, bottom) => {
const rc = context.getBoundingClientRect();
const rs = element.getBoundingClientRect();
element.style.top = clamp(0, rc.height - rs.height, top - rc.top + offset) + 'px';
});
}
Insert cell
function observeViewport(callback) {
let top = -1, bottom = -1, raf;
const observer = new IntersectionObserver(([e]) => {
const t = e.intersectionRect.top;
const b = e.intersectionRect.bottom;
if(t !== top || b !== bottom) {
top = t;
bottom = b;
callback(top, bottom);
}
observer.unobserve(target);
raf = requestAnimationFrame(observe);
});
const target = document.documentElement;
const observe = () => observer.observe(target);
observe();
// Disconnect callback.
return () => {
cancelAnimationFrame(raf);
observer.disconnect();
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more