So, how do I know the scroll direction when the event it's triggered?
In the returned object the closest possibility I see is interacting with the boundingClientRect
kind of saving the last scroll position but I don't know if handling boundingClientRect
will end up on performance issues.
Is it possible to use the intersection event to figure out the scroll direction (up / down)?
I have added this basic snippet, so if someone can help me.
I will be very thankful.
Here is the snippet:
var options = {
rootMargin: '0px',
threshold: 1.0
}
function callback(entries, observer) {
entries.forEach(entry => {
if (entry.isIntersecting) {
console.log('entry', entry);
}
});
};
var elementToObserve = document.querySelector('#element');
var observer = new IntersectionObserver(callback, options);
observer.observe(elementToObserve);
#element {
margin: 1500px auto;
width: 150px;
height: 150px;
background: #ccc;
color: white;
font-family: sans-serif;
font-weight: 100;
font-size: 25px;
text-align: center;
line-height: 150px;
}
<div id="element">Observed</div>
I would like to know this, so I can apply this on fixed headers menu to show/hide it
boundingClientRect
passed inside theIntersectionObserverEntry
already, whether you're asking for it or not. Any "performance penalty" calculating the bounding client rectangle entails has already occurred, nothing you could do about that. So you might as well make use of the top or bottom offset that was calculated already and compare it to the stored previous value, instead of letting the effort that's already been made go to waste ...