23 lines
544 B
JavaScript
23 lines
544 B
JavaScript
export const isSticky = (el, scrollY, stickyTop) => {
|
|
const top = Math.floor(el.offsetTop - scrollY);
|
|
|
|
if (top <= stickyTop) {
|
|
el.classList.add('is-stuck');
|
|
} else {
|
|
el.classList.remove('is-stuck');
|
|
}
|
|
};
|
|
|
|
export default (el) => {
|
|
if (!el) return;
|
|
|
|
const computedStyle = window.getComputedStyle(el);
|
|
|
|
if (!/sticky/.test(computedStyle.position)) return;
|
|
|
|
const stickyTop = parseInt(computedStyle.top, 10);
|
|
|
|
document.addEventListener('scroll', () => isSticky(el, window.scrollY, stickyTop), {
|
|
passive: true,
|
|
});
|
|
};
|