Constimport {html, defineElement, onIntersect} from 'element-vir';
const MyElement = defineElement()({
tagName: 'my-element',
render() {
return html`
<div
${onIntersect({threshold: 1}, ({element, entry}) => {
if (entry.isIntersecting) {
console.log('is intersecting!');
} else {
console.log('is not intersecting');
}
})}
>
Some div
</div>
`;
},
});
A directive that fires its listener any time the element's configured "intersection" is crossed. This is commonly use for detecting when an element has scrolled into view. This uses the built-in
IntersectionObserverAPI, so it is very efficient.